k-mean 拐点

n = 100
g = 6
set.seed(g)
d <- data.frame(x = unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))),
                y = unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))))
plot(d)
###################
d = read.table('clipboard',header = T)
plot(d)

mydata <- d
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(mydata,
                                     centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
     ylab="Within groups sum of squares")


library(fpc)
pamk.best <- pamk(d)
##############################
lastcluster = pam(d, 1) # pam(d, pamk.best$nc)
plot(d,type='l')
vl = c(lastcluster$medoids[,1])
vl
abline(v=vl,lty=2,col='red')

#library(cluster)
#plot(pam(d, 3))
###################################
#cat("number of clusters estimated by
#    optimum average silhouette width:", pamk.best$nc, "\n")
#library(cluster)
#plot(pam(d, pamk.best$nc))




猜你喜欢

转载自www.cnblogs.com/arcserver/p/9186004.html