본문 바로가기

Data Mining & R

Clustering - K-means 예시

R을 이용한 k-평균 군집화 - Iris

이번 예제에서는 R에서 제공하는 Iris 데이터를 이용하여 k-평균 군집화(k-means clustering)를 수행해 본다.

데이터 - Iris

Iris 데이터는 다음과 같이 구성되어 있다.

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
plot(iris)

plot of chunk unnamed-chunk-1

종(Species) 정보를 제외하고 꽃잎과 꽃받침의 길이와 두께 정보만을 이용하여 군집화를 수행해 보도록 한다. scale() 함수를 이용하여 변수의 값을 표준화하였다.

iris.data <- scale(iris[-5])
summary(iris.data)
##   Sepal.Length      Sepal.Width      Petal.Length     Petal.Width    
##  Min.   :-1.8638   Min.   :-2.426   Min.   :-1.562   Min.   :-1.442  
##  1st Qu.:-0.8977   1st Qu.:-0.590   1st Qu.:-1.222   1st Qu.:-1.180  
##  Median :-0.0523   Median :-0.132   Median : 0.335   Median : 0.132  
##  Mean   : 0.0000   Mean   : 0.000   Mean   : 0.000   Mean   : 0.000  
##  3rd Qu.: 0.6722   3rd Qu.: 0.557   3rd Qu.: 0.760   3rd Qu.: 0.788  
##  Max.   : 2.4837   Max.   : 3.080   Max.   : 1.780   Max.   : 1.706

K-평균 군집화

K-평 군집화를 하기 위해서는 stats 패키지에서 제공하는 kmeans() 함수를 이용해야 한다. 이 때 hclust의 첫째 인자는 요소는 데이터이고, 두째 인자는 간의 군집 수(k)여야 한다.

K=3인 경우

iris 데이터는 3개의 종으로 구성된 데이터이므로 군집의 수를 3개로 해서 군집화를 시도해 본다.

(iris.kmeans <- kmeans(iris.data, centers = 3, nstart = 5))
## K-means clustering with 3 clusters of sizes 50, 47, 53
## 
## Cluster means:
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1     -1.01119     0.85041      -1.3006     -1.2507
## 2      1.13218     0.08813       0.9928      1.0141
## 3     -0.05005    -0.88043       0.3466      0.2806
## 
## Clustering vector:
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [36] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 3 3 3 2 3 3 3 3 3 3 3 3 2 3 3 3 3
##  [71] 2 3 3 3 3 2 2 2 3 3 3 3 3 3 3 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 2 2 2
## [106] 2 3 2 2 2 2 2 2 3 3 2 2 2 2 3 2 3 2 3 2 2 3 2 2 2 2 2 2 3 3 2 2 2 3 2
## [141] 2 2 3 2 2 2 3 2 2 3
## 
## Within cluster sum of squares by cluster:
## [1] 47.35 47.45 44.09
##  (between_SS / total_SS =  76.7 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"    
## [5] "tot.withinss" "betweenss"    "size"         "iter"        
## [9] "ifault"

결과에서 47, 50, 53개의 데이터로 구성된 3개의 군집이 만들어졌음을 볼 수 있다. kmeans() 함수의 결과로 cluster, centers 등 다양한 요소가 생성되었음을 볼 수 있다. cluster는 데이터 각 요소가 어떤 군집으로 분류되었는지에 대한 정보를 준다.

iris.kmeans$cluster
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [36] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 3 3 3 2 3 3 3 3 3 3 3 3 2 3 3 3 3
##  [71] 2 3 3 3 3 2 2 2 3 3 3 3 3 3 3 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 2 2 2
## [106] 2 3 2 2 2 2 2 2 3 3 2 2 2 2 3 2 3 2 3 2 2 3 2 2 2 2 2 2 3 3 2 2 2 3 2
## [141] 2 2 3 2 2 2 3 2 2 3
iris.kmeans$iter
## [1] 3

이 정보를 이용하여 plot() 함수를 이용하면 iris 데이터와 군집간의 관계를 살펴보자.

plot(iris[-5], pch = iris.kmeans$cluster, col = iris.kmeans$cluster)

plot of chunk unnamed-chunk-5

세개로 나누어진 군집과 종의 교차표는 다음과 같다. 결과에서 알 수 잇듯이 setosa는 하나의 군집 잘 분리되나 versicolor와 virginica가 두개의 군집으로 섞여서 분리되었음을 볼 수 있다.

table(iris.kmeans$cluster, iris$Species)
##    
##     setosa versicolor virginica
##   1     50          0         0
##   2      0         11        36
##   3      0         39        14

k=4인 경우

(iris.kmeans <- kmeans(iris.data, centers = 4, nstart = 5))
## K-means clustering with 4 clusters of sizes 25, 47, 25, 53
## 
## Cluster means:
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1     -1.30344     0.19884      -1.3040     -1.2848
## 2      1.13218     0.08813       0.9928      1.0141
## 3     -0.71894     1.50199      -1.2972     -1.2166
## 4     -0.05005    -0.88043       0.3466      0.2806
## 
## Clustering vector:
##   [1] 3 1 1 1 3 3 1 1 1 1 3 1 1 1 3 3 3 3 3 3 3 3 3 1 1 1 1 3 3 1 1 3 3 3 1
##  [36] 1 3 3 1 1 3 1 1 3 3 1 3 1 3 1 2 2 2 4 4 4 2 4 4 4 4 4 4 4 4 2 4 4 4 4
##  [71] 2 4 4 4 4 2 2 2 4 4 4 4 4 4 4 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 2 4 2 2 2
## [106] 2 4 2 2 2 2 2 2 4 4 2 2 2 2 4 2 4 2 4 2 2 4 2 2 2 2 2 2 4 4 2 2 2 4 2
## [141] 2 2 4 2 2 2 4 2 2 4
## 
## Within cluster sum of squares by cluster:
## [1]  9.646 47.450 12.148 44.088
##  (between_SS / total_SS =  81.0 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"    
## [5] "tot.withinss" "betweenss"    "size"         "iter"        
## [9] "ifault"
plot(iris[-5], pch = iris.kmeans$cluster, col = iris.kmeans$cluster)

plot of chunk unnamed-chunk-7

table(iris.kmeans$cluster, iris$Species)
##    
##     setosa versicolor virginica
##   1     25          0         0
##   2      0         11        36
##   3     25          0         0
##   4      0         39        14

결과에서 보듯이 군집간의 거리의 제곱의 합(between_SS)의 전체 거리의 제곱합(total_SS)에서 81%를 차지함을 알 수 있다. k=3인 경우 76.7 %였으므로 큰 향상은 없었음을 볼 수 있다.

지금 예처럼 k에 따라 군집의 결과가 차이를 보일 수 있으므로 여러 숫자를 적용해 본 후 최적의 군집화 결과를 도출하는 것이 좋다.



출처 : http://blog.daum.net/sys4ppl/7

'Data Mining & R' 카테고리의 다른 글

k Nearest Neighbors(KNN)  (0) 2016.04.20
Deep Neural Network (DNN)  (0) 2016.04.19
Classification - Decision Tree 예시  (0) 2016.04.17
Clustering - K-means 예시2  (0) 2016.04.17
DataSet 제공 홈페이지  (0) 2016.04.17