01. ๋ค์์ 15๋ช ์ ๋ฉด์ ์๋ฅผ ๋์์ผ๋ก ๊ฐ์น๊ด, ์ ๋ฌธ์ง์, ์๊ฒฉ์ฆ ์ ๋ฌด ๋ฑ์ ํ ๋๋ก ์ข ํฉ์ ์์ ๊ทผ๊ฑฐํ์ฌ ํฉ๊ฒฉ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ ์๋ฃ์ด๋ค. ๋ค์๊ณผ ๊ฐ์ ๋จ๊ณ๋ก ๊ณ์ธต์ ๊ตฐ์ง๋ถ์์ ์ํํ์ฌ ๊ตฐ์ง์(cluster)๋ฅผ ํ์ํ๊ณ , ๊ฐ ๊ตฐ์ง๋ณ๋ก ์๋ธ์ ์ ์์ฑํ์ฌ ๊ฐ ๊ตฐ์ง์ ํน์ฑ์ ๋ถ์ํ์์ค.
setwd('c:/ITWILL/2_Rwork/data')
๋จ๊ณ1 : dataset ๊ฐ์ ธ์ค๊ธฐ
interview = read.csv("interview.csv")
names(interview) # ๋ณ์๋ช
head(interview)
๋จ๊ณ2 : ์ ํด๋ฆฌ๋์ ๊ฑฐ๋ฆฌ ๊ณ์ฐ
inter_df = interview[c(2:8)] # ์์๋ฒํธ์ ํฉ๊ฒฉ์ฌ๋ถ ์ ์ธ
idist <- dist(inter_df) # ์ ํด๋ฆฌ๋์ ๊ฑฐ๋ฆฌ ์์ฑ
head(idist)
๋จ๊ณ3 : ๊ณ์ธต์ ๊ตฐ์ง๋ถ์ & ๋ด๋๋ก๊ทธ๋จ ์๊ฐํ
hc <- hclust(idist)
hc
plot(hc, hang=-1) # ์์๊ฐ ์ ์ธ
rect.hclust(hc, k=3, border="red") # 3๊ฐ ๊ทธ๋ฃน์
๋จ๊ณ4 : ๊ตฐ์ง๋ณ ์๋ธ์
๋ง๋ค๊ธฐ : cutree()ํจ์ ์ด์ฉ
ghc = cutree(hc, k=3)
inter_df$ghc <- ghc
inter_df
g1 = subset(inter_df, ghc==1)
g2 = subset(inter_df, ghc==2)
g3 = subset(inter_df, ghc==3)
๋จ๊ณ5 : ๊ฐ ๊ตฐ์ง๋ณ ํน์ฑ ๋ถ์ : summary()ํจ์ ์ด์ฉ
summary(g1) # ์๊ฒฉ์ฆ : 1, Mean :19 Mean :14.4 Mean :15.6 Mean :14.8 Mean :11.8 Mean :75.6
summary(g2) # ์๊ฒฉ์ฆ : 0 or 1, Mean :11 Mean :15.2 Mean :19.4 Mean :11, Mean :6.2 Mean :62.8
summary(g3) # ์๊ฒฉ์ฆ : 0, Mean :14.4 Mean :18.8 Mean :10.8 Mean : 9.4, Mean :18.2 Mean :71.6
g1
interview[c(1:2,4,6,13), ]
02. ๋ค์๊ณผ ๊ฐ์ ์กฐ๊ฑด์ ์ด์ฉํ์ฌ ๊ฐ ๋จ๊ณ๋ณ๋ก ๋น๊ณ์ธต์ ๊ตฐ์ง๋ถ์์ ์ํํ์์ค.
์กฐ๊ฑด1) ๋์ ํ์ผ : c:/Rwork/Part-IV/product_sales.csv
์กฐ๊ฑด2) ๋ณ์ ์ค๋ช
: tot_price : ์ด๊ตฌ๋งค์ก, buy_count : ๊ตฌ๋งคํ์, visit_count : ๋งค์ฅ๋ฐฉ๋ฌธํ์, avg_price : ํ๊ท ๊ตฌ๋งค์ก
sales <- read.csv("product_sales.csv", header=TRUE)
head(sales)
๋จ๊ณ1: ๋น๊ณ์ธต์ ๊ตฐ์ง๋ถ์ : 3๊ฐ ๊ตฐ์ง์ผ๋ก ๊ตฐ์งํ
model = kmeans(sales, centers = 3)
model
sizes 38, 50, 62
Within cluster sum of squares by cluster: ์์ง๋
[1] 23.88395 22.51380 39.95968
(between_SS / total_SS = 87.4 %) : ๋ชจ๋ธ ์ฐ์์ฑ
๋จ๊ณ2: ์ํ๋ฐ์ดํฐ์ ๊ตฐ์ง์ ์ถ๊ฐ
sales$cluster = model$cluster # ํด๋ฌ์คํฐ ์ ๋ณด ์ถ๊ฐ
head(sales)
๋จ๊ณ3 : tot_price ๋ณ์์ ๊ฐ์ฅ ์๊ด๊ณ์๊ฐ ๋์ ๋ณ์์ ๊ตฐ์ง๋ถ์ ์๊ฐํ
(1) ์๊ด๊ด๊ณ ๋ถ์ : cor()
cor(sales) # 0.87175416 : tot_price vs avg_price
(2) ๋น๊ณ์ธต์ ๊ตฐ์ง๋ถ์ ์๊ฐํ : ๊ทธ๋ฃน์ผ๋ก ์์ ํ์
plot(sales$tot_price ~ sales$avg_price, col = sales$cluster)
๋จ๊ณ4. ๊ตฐ์ง์ ์ค์ฌ์ ํ์
model$centers # ๊ฐ ๊ตฐ์ง์ ๋ณ์ ํ๊ท
points(model$centers[, c('avg_price', 'tot_price')], col=c(3,1,2),
pch=8, cex=5)
[ํด์ค] avg_price ๋ณ์๊ฐ tot_price ๋ณ์์ ๋นํด์ ๊ตฐ์ง์ ์ํฅ๋ ๋์
library(factoextra) # fviz_cluster()
fviz_cluster(model, data = sales)
Dim1(57.8%) : ๊ณตํ๋ 58% -> avg_price ์ฃผ์ฑ๋ถ
Dim2(27.6%) : ๊ณตํ๋ 28%
sales[110, ]
tot_price visit_count buy_count avg_price cluster
110 6.1 1.4 2.6 5.6 1 -> cluster1
sales[36, ]
36 5 1 2 3.5 3 -> cluster3
sales[17, ]
17 4.5 0.3 2.3 1.3 2 -> cluster2
๊ฐ ๊ตฐ์ง๋ณ ํน์ฑ ๋ถ์(x์ถ ๊ธฐ์ค : avg_price)
cluster1 : ์ด๊ตฌ๋งค๊ธ์ก๊ณผ ํ๊ท ๊ตฌ๋งค์ก ๊ฐ์ฅ ๋์ ๊ทธ๋ฃน
cluster2 : ์ด๊ตฌ๋งค๊ธ์ก๊ณผ ํ๊ท ๊ตฌ๋งค์ก ๊ฐ์ฅ ๋ฎ์ ๊ทธ๋ฃน
cluster3 : ์ด๊ตฌ๋งค๊ธ์ก๊ณผ ํ๊ท ๊ตฌ๋งค์ก ์ค๊ฐ์ธ ๊ทธ๋ฃน
'๊ฐ์ธ๊ณต๋ถ > R' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
39. R ์์๋ธ๋ชจ๋ธ ์ฐ์ต๋ฌธ์ (0) | 2021.10.25 |
---|---|
36. R ์ฐ๊ด๋ถ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.22 |
34. R ๋ถ๋ฅ๋ถ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.19 |
33. R ๋ก์ง์คํฑํ๊ท๋ถ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.18 |
31. R ์ ํํ๊ท๋ถ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.15 |