76. Python Cluster&Recommand ์ฐ์ต๋ฌธ์
๋ฌธ) ์ ์ ์ฌ์ ๋ฉด์ ์ํ(interview.csv) ๋ฐ์ดํฐ ์ ์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ๊ตฐ์ง๋ชจ๋ธ์ ์์ฑํ์์ค.
<์กฐ๊ฑด1> ๋์์นผ๋ผ : ๊ฐ์น๊ด,์ ๋ฌธ์ง์,๋ฐํ๋ ฅ,์ธ์ฑ,์ฐฝ์๋ ฅ,์๊ฒฉ์ฆ,์ข ํฉ์ ์
<์กฐ๊ฑด2> ๊ณ์ธต์ ๊ตฐ์ง๋ถ์์ ์์ ์ฐ๊ฒฐ๋ฐฉ์ ์ ์ฉ
<์กฐ๊ฑด3> ๋ด๋๋ก๊ทธ๋จ ์๊ฐํ
<์กฐ๊ฑด4> ํ ๋๋ก๊ทธ๋จ์ ๋ณด๊ณ 3๊ฐ ๊ตฐ์ง์ผ๋ก ์๋ธ์ ์์ฑ
import pandas as pd
from scipy.cluster.hierarchy import linkage, dendrogram #๊ณ์ธต์ ๊ตฐ์ง model
import matplotlib.pyplot as plt
from sklearn.preprocessing import minmax_scale
data loading - ์ ์
์ฌ์ ๋ฉด์ ์ํ ๋ฐ์ดํฐ ์
interview = pd.read_csv("c:/ITWILL/4_Python-2/data/interview.csv", encoding='ms949')
print(interview.info())
RangeIndex: 15 entries, 0 to 14
Data columns (total 9 columns):
<์กฐ๊ฑด1> subset ์์ฑ : no ์นผ๋ผ์ ์ ์ธํ ๋๋จธ์ง ์นผ๋ผ ์ด์ฉ
cols = list(interview.columns)
interviewDF = interview[cols[1:-1]]
print(interviewDF)
interviewDF = minmax_scale(interviewDF)
<์กฐ๊ฑด2> ๊ณ์ธต์ ๊ตฐ์ง ๋ถ์ ์์ ์ฐ๊ฒฐ๋ฐฉ์ - ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ์ ํด๋ฌ์คํฐ๋ฅผ ๋์์ผ๋ก ๊ฑฐ๋ฆฌ ์ธก์ ํ๋ ๋ฐฉ์
clusters = linkage(interviewDF, method='complete')
<์กฐ๊ฑด3> ๋ด๋๋ก๊ทธ๋จ ์๊ฐํ : ๊ตฐ์ง์๋ ์ฌ์ฉ ๊ฒฐ์
plt.figure(figsize=(15, 5))
dendrogram(clusters,
leaf_rotation=90,
leaf_font_size=20,)
plt.show()
cluster1 = 8,2,10,4,13
cluster2 = 12,1,5,0,3
cluster3 = 14,7,9,6,11
4. ํด๋ฌ์คํฐ๋ง ์๋ฅด๊ธฐ/ํ๊ฐ : y ๋ณ์ -> 3๊ฐ ํด๋ฌ์คํฐ๋ง
from scipy.cluster.hierarchy import fcluster #ํด๋ฌ์คํฐ ์๋ฅด๊ธฐ
1) ํด๋ฌ์คํฐ ์๋ฅด๊ธฐ ๊ฐ์ฒด ์์ฑ : ํ
๋๋ก๊ทธ๋จ ๊ฒฐ๊ณผ ํ๊ฐ
re_clusters = fcluster(clusters, t=3, criterion='maxclust')
print(re_clusters)
2) ์นผ๋ผ์ผ๋ก ์ถ๊ฐ
interview['cluster'] = re_clusters
interview.head()
interview.tail()
no ๊ฐ์น๊ด ์ ๋ฌธ์ง์ ๋ฐํ๋ ฅ ์ธ์ฑ ์ฐฝ์๋ ฅ ์๊ฒฉ์ฆ ์ข
ํฉ์ ์ ํฉ๊ฒฉ์ฌ๋ถ cluster
10 111 13 14 19 12 8 0 66 ๋ถํฉ๊ฒฉ 1
11 112 14 20 11 9 16 0 70 ๋ถํฉ๊ฒฉ 2
12 113 18 14 16 12 10 1 70 ํฉ๊ฒฉ 3
13 114 10 13 18 10 6 0 57 ๋ถํฉ๊ฒฉ 1
14 115 13 17 10 8 17 0 65 ๋ถํฉ๊ฒฉ 2
๋ฌธ2) ์๋์ ๊ฐ์ ๋จ๊ณ๋ก kMeans ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํ์ฌ ํ์ธ์ ๊ตฐ์ง๋ถ์์ ์ํํ์์ค.
<์กฐ๊ฑด> ๋ณ์ ์ค๋ช : tot_price : ์ด๊ตฌ๋งค์ก, buy_count : ๊ตฌ๋งคํ์, visit_count : ๋งค์ฅ๋ฐฉ๋ฌธํ์, avg_price : ํ๊ท ๊ตฌ๋งค์ก
๋จ๊ณ1 : 3๊ฐ ๊ตฐ์ง์ผ๋ก ๊ตฐ์งํ
๋จ๊ณ2: ์ํ๋ฐ์ดํฐ์ ๊ตฐ์ง ์์ธก์น ์ถ๊ฐ
๋จ๊ณ3 : tot_price ๋ณ์์ ๊ฐ์ฅ ์๊ด๊ณ์๊ฐ ๋์ ๋ณ์๋ก ์ฐ์ ๋(์์ : ํด๋ฌ์คํฐ ๊ฒฐ๊ณผ)
๋จ๊ณ4 : ์ฐ์ ๋์ ๊ตฐ์ง์ ์ค์ฌ์ ์๊ฐํ
๋จ๊ณ5 : ๊ตฐ์ง๋ณ ํน์ฑ๋ถ์ : ์ฐ์๊ณ ๊ฐ ๊ตฐ์ง ์๋ณ
import pandas as pd
from sklearn.cluster import KMeans # kMeans model
import matplotlib.pyplot as plt
sales = pd.read_csv(r"C:\ITWILL\4_Python-2/data/product_sales.csv")
print(sales.info())
RangeIndex: 150 entries, 0 to 149
Data columns (total 4 columns):
tot_price 150 non-null float64 -> ์ด๊ตฌ๋งค๊ธ์ก
visit_count 150 non-null float64 -> ๋งค์ฅ๋ฐฉ๋ฌธ์
buy_count 150 non-null float64 -> ๊ตฌ๋งคํ์
avg_price 150 non-null float64 -> ํ๊ท ๊ตฌ๋งค๊ธ์ก
model = KMeans(n_clusters=3, random_state=0, algorithm='auto') #k=3, auto:default
model.fit(sales)
kMeans model ์์ธก์น
pred = model.predict(sales)
print(pred)
์์ธก์น ์ถ๊ฐ
sales['predict'] = pred #column ์ถ๊ฐ = numpy vector ์ถ๊ฐ ๊ฐ๋ฅ
print(sales)
์๊ด๊ณ์
print(sales.corr()) #tot_price vs avg_price
tot_price vs avg_price ์ฐ์ ๋
plt.scatter(sales['tot_price'], sales['avg_price'], c=sales.iloc[:,4])
๊ตฐ์ง ์ค์๊ฐ
centers = model.cluster_centers_
print(centers)
[[6.83902439 5.67804878]
[5.00784314 1.49215686]
[5.87413793 4.39310345]]
์ค์๊ฐ ์๊ฐํ
plt.scatter(centers[:,0], centers[:, 3], marker='D', c='r')
plt.show()
[์ถ๊ฐ] ๊ฐ ๊ตฐ์ง๋ณ ํน์ฑ๋ถ์(ํต๊ณ)
ales_g = sales.groupby('predict')
sales_g.size()
'''
0 50
1 62
2 38
'''
print(sales_g.mean())
tot_price visit_count buy_count avg_price
predict
0 5.006000 0.244000 3.284000 1.464000
1 5.901613 1.433871 2.754839 4.393548
2 6.850000 2.071053 3.071053 5.742105 -> ์ฐ์๊ณ ๊ฐ