핀아의 저장소 ( •̀ ω •́ )✧

45일차(2021-1-21) 본문

Big Data/데이터 분석

45일차(2021-1-21)

_핀아_ 2021. 1. 21. 20:59

iris 데이터를 이용하여 군집분석 실습해보기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#데이터 불러오기 및 전처리
data("iris")
iris2<-iris[,1:4#y값 목표변수 (Species) 제외
 
#군집분석 시 무조건 고려해서 넣는 값
km.out.withness<-c() #군집 내 제곱합 저장
km.out.between<-c() #군집 간 제곱합 저장
for (i in 2:7){ #군집수를 k=2~7까지 변화시켜가며 클러스터링 시행
  set.seed(1)
  km.out<-kmeans(iris2, centers=i)
  km.out.withness[i-1]<-km.out$tot.withinss #군집 내 제곱합 저장
  km.out.between[i-1]<-km.out$betweenss #군집 간 제곱합 저장
  }
data.frame(km.out.withness, km.out.between)
 
#자료값 확인하기
summary(km.out)
cs

 


1
2
3
4
5
km.out.k3<-kmeans(iris2, centers=3 )
km.out.k3$centers #각 군집의 중심점 출력
km.out.k3$cluster #각 관측치의 할당된 군집번호 출력
km.out.k3$size #각 군집의 데이터 관측치 개수 출력
table(km.out.k3$cluster, iris$Species) #군집결과와 원래 품종 개수 비교
cs

1
2
#그래프 그리기
plot(iris2[,1:2], col=km.out.k3$cluster, pch=ifelse(km.out.k3$cluster==116, ifelse(km.out.k3$cluster==21718)), cex=2) ; points(km.out.k3$centers, col=1:3, pch=16:18, cex=5)
cs

 

 

 

 

 

 

 

 

'Big Data > 데이터 분석' 카테고리의 다른 글

43일차(2021-1-11)  (0) 2021.01.11
42일차(2021-1-8)  (0) 2021.01.09
41일차(2021-1-7)  (0) 2021.01.07
40일차(2021-1-5)  (0) 2021.01.05
39일차(2021-1-4)  (0) 2021.01.05
Comments