Contents

1 k-means k-medioids 谿伎
2
3 豕 k襯 谿城 るジ 覦覯
4 谿瑚襭


1 k-means k-medioids 谿伎 #

  • k-means : 譴朱 ′
  • k-medioids: れ 螳 譴朱 ′

2 #

一危
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))))

library(fpc)
library("cluster")
#豕 k螳 谿場

#k <- pamk(d)$nc
k <- pamk(d)$nc+1
cl <- pam(d, k)
result <- data.frame(d, clusterid=cl$clustering)

par(mfrow=c(1,2))

plot(d, cex=0.2)
plot(d, cex=0.2)
colour <- c("red", "blue", "black", "green")
points(result$x, result$y, col=c(colour[result$clusterid]))

par(mfrow=c(1,1))
kmedoids.png


3 豕 k襯 谿城 るジ 覦覯 #

谿瑚: http://stackoverflow.com/questions/15376075/cluster-analysis-in-r-determine-the-optimal-number-of-clusters
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))))

sim <- clusGap(d, FUN = pam, K.max = 20, B = 100)
k <- with(sim, maxSE(Tab[,"gap"],Tab[,"SE.sim"]))
plot(sim)
abline(v=k, lty=2, col="blue")

k.png

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

4 谿瑚襭 #