59doit
[ R ] 군집분석 연습문제 본문
반응형
1. iris데이터 셋이 1-4번째 변수를 대상으로 유클리드 거리 매트릭스를 구하여 idist에 저 장한 후 계층적 클러스터링을 적용하여 결과를 시각화하시오
library(cluster) data_iris <- iris data_irisdf <- data_iris[c(1:4)] # 1-4번째 변수를 대상 data_irisdf |
1) 유클리드 거리 계산
idist <- dist(data_irisdf,method = "euclidean") idist head(idist) # [1] 0.5385165 0.5099020 0.6480741 0.1414214 0.6164414 0.5196152 |
2) 계층형 군집분석(클러스터링
hc <- hclust(idist) hc # Call: # hclust(d = idist) # # Cluster method : complete # Distance : euclidean # Number of objects: 150 |
3) 분류결과를 대상으로 음수값을 제거하여 덴드로그램 시각화
x11() # 다른 창에서 보기 plot(hc,hang=-1) ![]() |
cf) x11() # 다른 창에서 볼 수 있음,
4) 그룹 수를 4개로 지정하고 그룹별로 테두리 표시
rect.hclust(hc, k = 4, border ="red")![]() |
2. product_sales.csv 내 데이터를 이용하여 단계별로 비계층적 군집 분석을 수행하시오
library(ggplot2) data <- read.csv("C:/product_sales.csv", header=T) data head(data) # tot_price visit_count buy_count avg_price # 1 5.0 0.2 3.0 1.6 # 2 5.0 0.4 3.4 1.6 # 3 5.2 0.2 3.5 1.5 # 4 5.2 0.2 3.0 1.4 # 5 4.7 0.2 3.2 1.6 # 6 4.8 0.2 3.1 1.6 |
1) 비계층적 군집분석: 3개의 군집으로 군집화
result <- kmeans(data, 3) result # K-means clustering with 3 clusters of sizes 50, 62, 38 # # Cluster means: # tot_price visit_count buy_count avg_price # 1 5.006000 0.244000 3.284000 1.464000 # 2 5.901613 1.433871 2.754839 4.393548 # 3 6.850000 2.071053 3.071053 5.742105 # # 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 2 2 3 2 2 2 2 2 2 # [35] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 # [69] 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3 3 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 # [103] 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2 3 3 2 1 1 1 1 1 1 1 1 1 1 1 # [137] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 # # Within cluster sum of squares by cluster: # [1] 22.51380 39.95968 23.88395 # (between_SS / total_SS = 87.4 %) # # Available components: # # [1] "cluster" "centers" "totss" "withinss" # [5] "tot.withinss" "betweenss" "size" "iter" # [9] "ifault" |
2) 원형데이터에 군집 수 추가
data$cluster <- result$cluster head(data) # tot_price visit_count buy_count avg_price cluster # 1 5.0 0.2 3.0 1.6 1 # 2 5.0 0.4 3.4 1.6 1 # 3 5.2 0.2 3.5 1.5 1 # 4 5.2 0.2 3.0 1.4 1 # 5 4.7 0.2 3.2 1.6 1 # 6 4.8 0.2 3.1 1.6 1 |
3) tot_price변수와 가장 상관계수가 높은 변수와 군집 분석 시각화
cor(data[ , -5], method = "pearson") # 가장 큰 수 : 가장 상관계수가 높은 변수 = avg_price # tot_price visit_ count buy_count avg_price # tot_price 1.00000000 0.8179536 -0.01305105 0.8717542 # visit_count 0.81795363 1.0000000 -0.23061166 0.9627571 # buy_count -0.01305105 -0.2306117 1.00000000 -0.2785045 # avg_price 0.87175416 0.9627571 -0.27850452 1.0000000 plot(data[c("tot_price", "avg_price")], col=data$cluster) ![]() |
4) 군집의 중심점 표시
plot(data$tot_price, data$avg_price , col = data$cluster) ![]() points(result$centers[ , c("tot_price", "avg_price")], col=1:3, pch = 8, cex = 5) ![]() |
반응형
'Q.' 카테고리의 다른 글
머신러닝 예제 TEST(8) (0) | 2022.12.05 |
---|---|
[ R ] 연관분석 연습문제 (0) | 2022.12.05 |
[ R ] 통계 기반 데이터 분석 예제 평가(7) (1) | 2022.11.30 |
[ R ] 다중회귀분석 연습문제 ch15 (0) | 2022.11.27 |
[ R ] ch14 연습문제 (0) | 2022.11.27 |
Comments