sdata = read.csv('sdata.txt')
library(cluster)
X = c(-4, 0, 4)
Y = c(10, 0, 10)
rnames = c("x1", "x2", "x3")
centers = data.frame(X, Y, row.names = rnames)
model = kmeans(sdata, centers = centers)
# model$centers
# model$cluster
# Calculate cohesion and separation
cohesion = model$tot.withinss
separation = model$betweenss
# Q1
cohesion
# Q2
separation
# Plot the data with the clusters
plot(sdata, col = model$cluster)
points(model$centers, col = 4, pch = "+", cex = 2)
# Calculate and plot silhouette
model_silhouette = silhouette(model$cluster, dist(sdata))
plot(model_silhouette)
mean_silhouette = mean(model_silhouette[, 3])
# Q3
mean_silhouette
X= c(-2, 2, 0)
Y = c(0, 0, 10)
rnames = c("x1", "x2", "x3")
centers = data.frame(X, Y, row.names = rnames)
model = kmeans(sdata, centers = centers)
model$centers
model$cluster
# Plot the data with the clusters
plot(sdata, col = model$cluster)
points(model$centers, col = 4, pch = "+", cex = 2)
Comments