1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# k-means 시각화
# 데이터 생성
<- c(3,4,1,5,7,9,5,4,6,8,4,5,9,8,7,8,6,7,2,1)
row <- c("A","B","C","D","E","F","G","H","I","J")
col <- c("X","Y")
data <- matrix( c, nrow= 10, ncol=2, byrow=TRUE, dimnames=list(row,col))
data
 
# 데이터 시각화
plot(data)
 
# k-means 패키지 불러오기
# install.packages("stats")
library(stats)
 
# k개 구하는 공식 : sqrt(n/2) n = 전체 행의 갯수
km <- kmeans(data,2)
km # K-means clustering with 2 clusters of sizes 5, 5
 
# 군집의 무게중심(중앙점) 확인
km$centers
 
# 군집화 확인
cbind(data,km$cluster)
 
# 군집의 무게중심 시각화
plot(round(km$center), col=km$cluster, pch=22, bg=km$center, xlim=range(0:10),ylim=range(0:10))
 
# 기존 데이터 
par(new=T)
plot( data, col=km$cluster, pch=22, xlim=range(0:10), ylim=range(0:10) )
cs

 

R로 시각화한 k-means

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# k-means 시각화
# 데이터 생성
<- c(3,4,1,5,7,9,5,4,6,8,4,5,9,8,7,8,6,7,2,1)
row <- c("A","B","C","D","E","F","G","H","I","J")
col <- c("X","Y")
data <- matrix( c, nrow= 10, ncol=2, byrow=TRUE, dimnames=list(row,col))
data
 
# 클러스터링 시각화 패키지 설치
# install.packages("factoextra")
library(factoextra)
 
km <- kmeans(data,2)
fviz_cluster(km, data = data, stand=F)
cs

 

패키지를 활용해 시각화한 k-means

 

 

 

+ Recent posts