K-means

K-means 알고리즘은 n개의 객체들의 집합을 K개의 군집으로 분해하는 거리에 기반을 둔 clustering 기법이다.

1. 특징

1.1. 장점

1.2. 단점

2. 단계

3. 예제

party package example by Zhao

## stats package를 이용하여 iris 데이터를 K-means 분석기법으로 분류해
## 본다.
library(stats)
## iris 데이터의 Species를 NULL값으로 지정하여 제거한 데이터를 사용한다.
data(iris)
iris2 <- iris
iris2$Species <- NULL
## Species를 제거한 iris2 데이터를 3개의 군집으로 분류한다.
kmeans.result <- kmeans(iris2, 3)
kmeans.result
## K-means clustering with 3 clusters of sizes 62, 50, 38
## 
## Cluster means:
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1        5.902       2.748        4.394       1.434
## 2        5.006       3.428        1.462       0.246
## 3        6.850       3.074        5.742       2.071
## 
## Clustering vector:
##   [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
##  [36] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [71] 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 3 3 3
## [106] 3 1 3 3 3 3 3 3 1 1 3 3 3 3 1 3 1 3 1 3 3 1 1 3 3 3 3 3 1 3 3 3 3 1 3
## [141] 3 3 1 3 3 3 1 3 3 1
## 
## Within cluster sum of squares by cluster:
## [1] 39.82 15.15 23.88
##  (between_SS / total_SS =  88.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"    
## [5] "tot.withinss" "betweenss"    "size"
## iris 데이터의 실제 Species와 K-means 분석기법으로 분류한 Species를
## 교차표로 출력한다.
table(iris$Species, kmeans.result$cluster)
##             
##               1  2  3
##   setosa      0 50  0
##   versicolor 48  0  2
##   virginica  14  0 36
## Sepal.Length와 Sepal.Width에 대하여 K-means 분석기법으로 분류한 결과를
## plot으로 출력한다.
plot(iris2[c("Sepal.Length", "Sepal.Width")], col = kmeans.result$cluster)
points(kmeans.result$centers[, c("Sepal.Length", "Sepal.Width")], col = 1:3, 
    pch = 8, cex = 2)

plot of chunk unnamed-chunk-5