STA 414/2104 Mar 25, 2010
k
-means clustering
km15 = kmeans(x[g==0,],5)
km25 = kmeans(x[g==1,],5)
for(i in 1:6831){
md = c(mydist(xnew[i,],km15$center[1,]),mydist(xnew[i,],km15$center[2,
mydist(xnew[i,],km15$center[3,]),mydist(xnew[i,],km15$center[4,]),
mydist(xnew[i,],km15$center[5,]),mydist(xnew[i,],km25$center[1,]),
mydist(xnew[i,],km25$center[2,]),mydist(xnew[i,],km25$center[3,]),
mydist(xnew[i,],km25$center[4,]),mydist(xnew[i,],km25$center[5,]))
mark = which(md == min(md))
nearest[i] = ifelse(mark <= 5, "blue", "orange")}
plot(xnew, type="n", xlab = "x1", ylab = "x2",
main= "kmeans with 5 cluster centers")
points(xnew, col=nearest, pch=".")
points(km25$centers, col="orange", pch=19, cex=2)
points(km15$centers, col="blue", pch=19, cex=2)
points(x, col= ifelse(g==0, "blue","orange"))
1 / 16