DAY55. Python Cluster&Recommand (๊ณ์ธต๊ตฐ์ง๋ถ์, KMeans, ์ถ์ฒ์์คํ ๋ชจํ)
๊ตฐ์ง ๋ถ์
์ ์ฌ๋๊ฐ ๋์ ๋ฐ์ดํฐ ๊ตฐ์งํ
์ ์ฌ๋(์ ํด๋ฆฌ๋์ ๊ฑฐ๋ฆฌ์)๊ฐ ๋์ ๋ฐ์ดํฐ๋ผ๋ฆฌ ๊ทธ๋ฃนํ
๊ณ์ธตํ ํด๋ฌ์คํฐ๋ง๊ณผ ๋น๊ณ์ธตํ ํด๋ฌ์คํฐ๋ง์ผ๋ก ๋ถ๋ฅ
- K-means : ๋น๊ณ์ธต์ ๊ตฐ์ง๋ถ์
- Hierarchical : ๊ณ์ธต์ ๊ตฐ์ง๋ถ์
๊ตฐ์ง๋ถ์ ํน์ง
์ข ์๋ณ์(y๋ณ์)๊ฐ ์๋ ๋ฐ์ดํฐ ๋ง์ด๋ ๊ธฐ๋ฒ
์ ์ฒด์ ์ธ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ํ์ ํ๋๋ฐ ์ด์ฉ
๊ด์ธก๋์ ๊ฐ ์ ์ฌ์ฑ์ ๊ธฐ์ด๋ก ๋น์ทํ ๊ฒ ๋ผ๋ฆฌ ๊ทธ๋ฃนํ(Clustering)
์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ ๊ธฐ๋ฐ ์ ์ฌ ๊ฐ์ฒด ๋ฌถ์ (์ ์ฌ์ฑ = ์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ)
* ๊ด์ธก๋์ p์ q์ ๋์ํ๋ ๋ณ๋๊ฐ์ ์ฐจ๊ฐ ์์ผ๋ฉด, ๋ ๊ด์ธก๋์์ ์ ์ฌํ๋ค๊ณ ์ ์ํ๋ ์
ex. ๊ณ ๊ฐ DB -> ์๊ณ ๋ฆฌ์ฆ ์ ์ฉ -> ํจํด ์ถ์ถ(rule) -> ๊ทผ๊ฑฐ๋ฆฌ ๋ชจ ํ์ผ๋ก ๊ตฐ์งํ์ฑ
๊ณ์ธต์ ๊ตฐ์ง๋ถ์(ํ์์ ), ๋น๊ณ์ธต์ ๊ตฐ์ง๋ถ์(ํ์ธ์ )
์ฃผ์ ์๊ณ ๋ฆฌ์ฆ : k-means, hierarchical
๋ถ์๊ฒฐ๊ณผ์ ๋ํ ๊ฐ์ค ๊ฒ์ ์์(ํ๋น์ฑ ๊ฒ์ฆ ๋ฐฉ๋ฒ ์์)
๋ถ์ผ : ์ฌํ๊ณผํ, ์์ฐ๊ณผํ, ๊ณตํ ๋ถ์ผ
์ฒ๋ : ๋ฑ๊ฐ, ๋น์จ์ฒ๋(์ฐ์์ ์ธ ์)
๊ณ์ธต์ ๊ตฐ์ง ๋ถ์ | ๋น ๊ณ์ธต์ ๊ตฐ์ง ๋ถ์ |
์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ๋ฅผ ์ด์ฉํ ๊ตฐ์ง๋ถ์ ๋ฐฉ๋ฒ ๊ณ์ธต์ (hierarchical)์ผ๋ก ๊ตฐ์ง ๊ฒฐ๊ณผ ๋์ถ ํ์์ ๊ตฐ์ง๋ถ์ |
ํ์ธ์ ๊ตฐ์ง๋ถ์ ๋ฐฉ๋ฒ ๊ณ์ธต์ ๊ตฐ์ง๋ถ์๋ฒ ๋ณด๋ค ์๋ ๋น ๋ฆ ๊ตฐ์ง์ ์๋ฅผ ์๊ณ ์๋ ๊ฒฝ์ฐ ์ด์ฉ K๋ ๋ฏธ๋ฆฌ ์ ํ๋ ๊ตฐ์ง ์ ๊ณ์ธต์ ๊ตฐ์งํ์ ๊ฒฐ๊ณผ์ ์๊ฑฐํ์ฌ ๊ตฐ์ง ์ ๊ฒฐ์ ๋ณ์๋ณด๋ค ๊ด์ธก๋์ ๊ตฐ์งํ์ ๋ง์ด ์ด์ฉ ๊ตฐ์ง์ ์ค์ฌ(Cluster Center) ์ฌ์ฉ์๊ฐ ์ ํจ |
๊ตฐ์งํ ๋ฐฉ์ 1. ๋จ์ผ๊ธฐ์ค๊ฒฐํฉ๋ฐฉ์ 2. ์์ ๊ธฐ์ค๊ฒฐํฉ๋ฐฉ์ 3. ํ๊ท ๊ธฐ์ค๊ฒฐํฉ๋ฐฉ์ |
k-ํ๊ท ๊ตฐ์ง๋ถ์ ์๊ณ ๋ฆฌ์ฆ |
๊ฒฐ๊ณผ : ๋ฒค๋๋ก๊ทธ๋จ |
๊ณ์ธต์ ๊ตฐ์ง๋ถ์(Hierarchical Clustering)
์ํฅ์(Bottom-up)์ผ๋ก ๊ณ์ธต์ ๊ตฐ์ง ํ์ฑ
์ ํด๋ฆฌ๋์ ๊ฑฐ๋ฆฌ๊ณ์ฐ์ ์ด์ฉ
์ซ์ํ ๋ณ์๋ง ์ฌ์ฉ
from sklearn.datasets import load_iris #dataset
import pandas as pd #DataFrame
from scipy.cluster.hierarchy import linkage, dendrogram #๊ตฐ์ง๋ถ์ tool
import matplotlib.pyplot as plt #์ฐ์ ๋ ์๊ฐํ
1. dataset loading
iris = load_iris() #Load the data
X = iris.data #x๋ณ์
y = iris.target #y๋ณ์(target) - ์ซ์ํ : ๊ฑฐ๋ฆฌ๊ณ์ฐ
numpy -> DataFrame
iris_df = pd.DataFrame(X, columns=iris.feature_names)
iris_df['species'] = y #target ์ถ๊ฐ
iris_df.info()
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 sepal length (cm) 150 non-null float64
1 sepal width (cm) 150 non-null float64
2 petal length (cm) 150 non-null float64
3 petal width (cm) 150 non-null float64
4 species 150 non-null int32 -> object์์ intํ์ผ๋ก
2. ๊ณ์ธต์ ๊ตฐ์ง๋ถ์
clusters = linkage(iris_df, method='complete')
method = 'complete' : default - ์์ ์ฐ๊ฒฐ
method = 'simple' : ๋จ์์ฐ๊ฒฐ
method = 'average' : ํ๊ท ์ฐ๊ฒฐ
print(clusters)
clusters.shape #(149, 4)
3. ํ
๋๋ก๊ทธ๋จ ์๊ฐํ : ๊ตฐ์ง์๋ ์ฌ์ฉ์๊ฐ ๊ฒฐ์
plt.figure(figsize = (25, 10))
dendrogram(clusters)
plt.show()
[ํด์] y์ถ์ ์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ๊ณ์ฐ์. ์ต์๋จ ๊ตฐ์ง์ด ๊ฐ์ฅ ๋จผ ๊ฒ. ๊ตฐ์ง์ ๊ฐ์๋ ๋ถ์์์ ๋ชซ.
4. ๊ตฐ์ง ์ ์๋ฅด๊ธฐ -> 3๊ฐ
cluster = fcluster(clusters, t=3, criterion='maxclust') #criterion : ๊ฐ๊น์ด ๊ฑฐ๋ฆฌ ๊ธฐ์ค์ผ๋ก ์๋ฅด๊ธฐ
cluster #1~3
len(cluster) #150
2) ์นผ๋ผ ์ถ๊ฐ
iris_df['cluster'] = cluster
print(iris_df)
iris_df.columns
3) ์ฐ์ ๋ ์๊ฐํ(x=1 ์นผ๋ผ, y=3 ์นผ๋ผ, c=cluster)
plt.scatter(x = iris_df['sepal length (cm)'],
y = iris_df['petal length (cm)'],
c = iris_df['cluster'])
plt.show
[ํด์] ์ข์ธก์์๋ถํฐ 1, 3, 2๋ฒ ๊ตฐ์ง ์์
5. ๊ตฐ์ง๋ณ ํน์ฑ ๋ถ์
1) ๊ทธ๋ฃน(clust) ๊ฐ์ฒด
cluster_g = iris_df.groupby('cluster')
cluster_g.size()
cluster
1 50
2 34
3 66
dtype: int64
2) ๊ตฐ์ง์ ํ๊ท
cluster_g.mean() #species(๋ฒ์ฃผํ ๋ณ์) : 1๋ฒ๊ตฐ์ง(0), 2๋ฒ๊ตฐ์ง(2), 3๋ฒ๊ตฐ์ง(1+2)
sepal length (cm) sepal width (cm) ... petal width (cm) species
cluster ...
1 5.006000 3.428000 ... 0.246000 0.000000
2 6.888235 3.100000 ... 2.123529 2.000000
3 5.939394 2.754545 ... 1.445455 1.242424
kMeans
kMeans์๊ณ ๋ฆฌ์ฆ
ํ์ธ์ ๊ตฐ์ง๋ถ์
๊ตฐ์ง ์ k๋ฅผ ์๊ณ ์๋ ๊ฒฝ์ฐ ์ด์ฉ
import pandas as pd #DataFrame
from sklearn.cluster import KMeans #kMeans model
import numpy as np #array
import matplotlib.pyplot as plt #์๊ฐํ
1. text file -> dataset ์์ฑ
file = open(r'C:\ITWILL\4_Python-2\data\testSet.txt')
lines = file.readlines() #list๋ฐํ
print(lines, len(lines)) #80๊ฐ
dataset = []
for line in lines : #'1.658985\t4.285136\n'
cols = line.split('\t') #'1.658985', '4.285136\n'
rows = [] #1ํ ์ ์ฅ
for col in cols : #'1.658985', '4.285136\n'
rows.append(float(col)) #floatํ ํ๋ณํ [1.658985, 4.285136]
dataset.append(rows) #[[1.658985, 4.285136],... [-4.905566, -2.911070]]
list -> numpy
dataset_arr = np.array(dataset)
dataset_arr.shape #(80, 2)
plt.scatter(x = dataset_arr[:,0], y = dataset_arr[:,1])
2. DF์์ฑ
data_df = pd.DataFrame(dataset_arr, columns = ['x', 'y'])
data_df
3. KMeans model
model = KMeans(n_clusters = 4, algorithm = 'auto')
model.fit(data_df) #dataset์ ์ฉ
kMeans model ์์ธก์น
pred = model.predict(data_df)
print(pred) #์ฐ์ ๋์์ 4๊ฐ(0~3)์ ๊ตฐ์ง์ผ๋ก ์ชผ๊ฐฐ์ ๋ ์์ธกํด์ ๋ฐํ
dir(model)
[3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0
1 2 3 0 1 2]
๊ฐ ๊ตฐ์ง ์ค์๊ฐ
centers = model.cluster_centers_
array([[-2.46154315, 2.78737555],
[ 2.80293085, -2.7315146 ],
[-3.38237045, -2.9473363 ],
[ 2.6265299 , 3.10868015]])
4. kMeans model ์๊ฐํ
data_df['predict'] = pred
data_df.info()
์ฐ์ ๋
plt.scatter(x = data_df['x'], y = data_df['y'],
c = data_df['predict'])
์ค์๊ฐ ์ถ๊ฐ
plt.scatter(x = centers[:, 0], y = centers[:, 1],
c = 'r', marker = 'D')
#marker : ์ฐ์ ๋ ๋ชจ์ Diamond
plt.show()
* k๊ฐ์ด 4์ธ ๊ฒฝ์ฐ. k๊ฐ์ ์กฐ์ ํ๊ณ ์ถ์ผ๋ฉด KMeans model ๋จ๊ณ์์ ์กฐ์
Best Cluster ์ฐพ๋ ๋ฐฉ๋ฒ
from sklearn.cluster import KMeans #model
from sklearn.datasets import load_iris #dataset
import matplotlib.pyplot as plt #์๊ฐํ
1. dataset load
X, y = load_iris(return_X_y=True)
print(X.shape) #(150, 4)
print(X)
2. kMeans model
obj = KMeans(n_clusters=3)
model = obj.fit(X)
model ์์ธก์น
pred = model.predict(X)
3. Best Cluster
size = range(1, 11) # k๊ฐ ๋ฒ์(1~10)
inertia = [] # ๊ตฐ์ง์ ์์ง๋
* inertia value
- ๊ตฐ์ง์ ์์ง๋๋ฅผ ๋ํ๋ด๋ ์์น(์์น๊ฐ ์์ ์๋ก ์์ง๋๊ฐ ์ข๋ค.)
- ์ค์ฌ์ (centroid)์์ ๊ตฐ์ง(cluster)๋ด ํฌ์ธํธ๊ฐ์ ๊ฑฐ๋ฆฌ ์ ๊ณฑ์ ํฉ
- center์๊ฐ ์์ ์๋ก ๊ฐ์ด ์ปค์ง๋ค.
for k in size :
obj = KMeans(n_clusters = k) # 1 ~ 10
model = obj.fit(X)
inertia.append(model.inertia_)
print(inertia)
plt.plot(size, inertia, '-o')
plt.xticks(size)
plt.show()
[ํด์] ๊ธ๊ฒฉํ ํ๊ฐํ ํ 3-5 ์ฌ์ด์์ ๋ณํ์ ํญ์ด ์๋งํ๋ค.
3-5์ฌ์ด์ ํด๋ฌ์คํฐ๋ก ๋ถ๋ฅํ๋ ๊ฒ์ด ์ ์ ํ๋ค๊ณ ํ๋จํ๋ค.
์ถ์ฒ์์คํ ๋ชจํ
์ถ์ฒ ์์คํ ?
์ ๋ณด ํํฐ๋ง (IF) ๊ธฐ์ ์ ์ผ์ข ์ผ๋ก, ํน์ ์ฌ์ฉ์๊ฐ ๊ด์ฌ์ ๊ฐ์ง๋งํ ์ ๋ณด (์ํ, ์์ , ์ฑ , ๋ด์ค, ์ด๋ฏธ์ง, ์น ํ์ด์ง ๋ฑ)๋ฅผ ์ถ์ฒํ๋ ์์คํ
์ถ์ฒ ์๊ณ ๋ฆฌ์ฆ
1. ํ์ ํํฐ๋ง(Collaborative Filtering : CF)
๊ตฌ๋งค/์๋น ํจํด์ด ๋น์ทํ ์ฌ์ฉ์๋ฅผ ํ ์ง๋จ์ผ๋ก ๋ณด๊ณ ๊ทธ ์ง๋จ์ ์ํ ์๋น์๋ค์ ์ทจํฅ์ ์ถ์ฒํ๋ ๋ฐฉ์
- UBCF(User Based CF) : ํจํด์ด ๋น์ทํ ์ฌ์ฉ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ํ(Item) ์ถ์ฒ ๋ฐฉ์
์ถ์ฒ ๋์์(Active User)์์ ์ ์ฌ๋(Correlation Match)๋ฅผ ์ธก์ ํด์ ๊ฐ์ฅ ์ ์ฌํ ์ฌ๋๋ค์ ํ๊ท ๊ฐ์ผ๋ก ์ถ์ฒ์ํ์ ๋์ถ
* ๊ฒฐ์ธก์น๊ฐ ๋ง์ผ๋ฉด ์ ๋ขฐ์ฑ์ด ๋ฎ์์ง ์ ์๋ค๋ ๋จ์ ์ด ์๋ค.
- IBCF(Item Based CF) : ์ํ(Item)์ ๊ธฐ๋ฐ์ผ๋ก ์ฐ๊ด์ฑ์ด ์๋ ์ํ(Item) ์ถ์ฒ ๋ฐฉ์
์ปคํผ, ์นดํ๋ผํ ๋ ๊ฐ์ฅ ๋น์ทํ ๋ฒกํฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์ปคํผ๋ฅผ ์์ ๋, ์นดํ๋ผํ ๋ฅผ ์ฌ๋ผ๊ณ ์ถ์ฒํ ์ ์๋ค.
๋ น์ฐจ๋ ์ปคํผ์ ์นดํ๋ผํ ์ ๋ค๋ฅด๋ฏ๋ก ์ ์ฌ๋๊ฐ ๋ฎ์ ์ถ์ฒํด์ฃผ์ง ์๋๋ค.
2. ๋ด์ฉ๊ธฐ๋ฐ ํํฐ๋ง(Content-Based Filtering : CB)
์๋น์๊ฐ ์๋นํ๋ ์ ํ ์ค ํ ์คํธ ์ ๋ณด๊ฐ ๋ง์ ์ ํ ๋์
๋ด์ค, ์ฑ ๋ฑ ํ ์คํธ์ ๋ด์ฉ์ ๋ถ์ํด์ ์ถ์ฒํ๋ ๋ฐฉ๋ฒ
ํ ์คํธ ์ค์์ ํํ์(๋ช ์ฌ, ๋์ฌ ๋ฑ)๋ฅผ ๋ถ์ํ์ฌ ํต์ฌ ํค์๋๋ฅผ ๋ถ์ํ๋ ๊ธฐ์ ์ด ํต์ฌ
3. ์ง์๊ธฐ๋ฐ ํํฐ๋ง(Knowledge-Based Filtering : KB)
ํน์ ๋ถ์ผ์ ๋ํ ์ ๋ฌธ๊ฐ์ ๋์์ ๋ฐ์์ ๊ทธ ๋ถ์ผ์ ๋ํ ์ ์ฒด์ ์ธ ์ง์๊ตฌ์กฐ๋ฅผ ๋ง๋ค๊ณ ์ด๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ
์ถ์ฒ ์์คํ ์ฌ๋ก
๋ทํ๋ฆญ์ค : ๊ณ ๊ฐ์ ์ํ ํ๊ฐ๋ฅผ ๋ฐํ์ผ๋ก ํน์ ๊ณ ๊ฐ์๊ฒ ์ํ ์ถ์ฒ
์๋ง์กด : ์ ํ ์น ํ์ด์ง ๋ฐฉ๋ฌธ ๊ธฐ๋ก, ์ผํ์ฅ๋ฐ๊ตฌ๋ ๊ธฐ๋ฅ, ๊ตฌ๋งค ์ํ ์ ํธ ๋ฑ ๋ค์ํ ์ ๋ณด๋ฅผ ํ ๋๋ก ์ถ์ฒ ์์คํ ๊ตฌํ
ํ์ ํํฐ๋ง์ ์ ์ฌ๋(Similarity) ๊ณ์ฐ ๋ฐฉ๋ฒ
์๊ด๊ณ์(Correlation coefficient) ์ ์ฌ๋ : Pearson ์๊ด๊ณ์ ์ด์ฉ
์ฝ์ฌ์ธ(Cosine) ์ ์ฌ๋ : ๋ ๋ฒกํฐ ์ฌ์ด์ ๊ฐ๋
์ ํด๋ฆฌ๋์(Euclidean) : ๊ฑฐ๋ฆฌ ๊ธฐ๋ฐ ์ ์ฌ๋ ๊ณ
Jaccard ์ ์ฌ๋ : ์ด์งํ ์๋ฃ(binary data) ๋์ ์ ์ฌ๋ ๊ณ์ฐ
ํน์ด๊ฐ ๋ถํด (SVD)
ํน์ด๊ฐ ํ๋ ฌ ๋ถํด(Singular Value Decomposition)
์ฐจ์์ถ์(dimension reduction) ๊ธฐ๋ฒ : ํน์ด๊ฐ ์์ฑ
ex. ํ๋ ฌ A(m x n) ๋ถํด
A(m์ฌ์ฉ์ X n์์ดํ )์ NULL๊ฐ ์์ธกํ๋ ๊ณผ์ ์ด ํน์ด๊ฐ ๋ถํด.
P(M์ฌ์ฉ์ x kํน์ด๊ฐ) * Q.T(kํน์ด๊ฐ x N์์ดํ )
* ํน์ด๊ฐ : ํ๋ ฌ์ ํน์ง์ ๋ํ๋ด๋ ๊ฐ. ์๊ณ ๋ฆฌ์ฆ ์ด์ฉ (์ฐจ์์ถ์์์ ์ฌ์ฉ๋จ ex. 5๊ฐ ์ฐจ์ -> 2๊ฐ ์ฐจ์ ์ถ์)
[์ค์ต] ์ํ ์ถ์ฒ ์์คํ ์๊ณ ๋ฆฌ์ฆ
์ถ์ฒ ๋์์ : Toby
์ ์ฌ๋ ํ์ = ๋ฏธ๊ด๋ ์ํํ์ * Toby์์ ์ ์ฌ๋
์ถ์ฒ ์ํ ์์ธก = ์ ์ฌ๋ ํ์ / Toby์์ ์ ์ฌ๋
import pandas as pd
1. ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
ratings = pd.read_csv('D:/ITWILL/4_Python-2/data/movie_rating.csv')
print(ratings) #ํ๊ฐ์[critic] ์ํ[title] ํ์ [rating]
2. pivot table ์์ฑ : row(์ํ์ ๋ชฉ), column(ํ๊ฐ์), cell(ํ์ )
print('movie_ratings')
movie_ratings = pd.pivot_table(ratings,
index = 'title',
columns = 'critic',
values = 'rating').reset_index()
print(movie_ratings) #default index ์ถ๊ฐ
3. ์ฌ์ฉ์ ์ ์ฌ๋ ๊ณ์ฐ(์๊ด๊ณ์ R) : ๋ฒํธ ํ ์ถ๊ฐ ํจ๊ณผ
sim_users = movie_ratings.corr().reset_index() #corr(method='pearson')
print(sim_users) #default index ์ถ๊ฐ
critic critic Claudia Gene Jack Lisa Mick Toby
0 Claudia 1.000000 0.314970 0.028571 0.566947 0.566947 0.893405
1 Gene 0.314970 1.000000 0.963796 0.396059 0.411765 0.381246
2 Jack 0.028571 0.963796 1.000000 0.747018 0.211289 0.662849
3 Lisa 0.566947 0.396059 0.747018 1.000000 0.594089 0.991241
4 Mick 0.566947 0.411765 0.211289 0.594089 1.000000 0.924473
5 Toby 0.893405 0.381246 0.662849 0.991241 0.924473 1.000000
4. Toby ๋ฏธ๊ด๋ ์ํ ์ถ์ถ
1) movie_ratings table์์ title, Toby ์นผ๋ผ์ผ๋ก subset ์์ฑ
toby_rating = movie_ratings[['title', 'Toby']] #index ์นผ๋ผ ์ถ๊ฐ
print(toby_rating)
์นผ๋ผ๋ช
๊ต์ฒด
toby_rating.columns=['title', 'rating']
print(toby_rating)
critic title rating
0 Just My NaN
1 Lady NaN
2 Snakes 4.5
3 Superman 4.0
4 The Night NaN
5 You Me 1.0
2) Toby ๋ฏธ๊ด๋ ์ํ์ ๋ชฉ ์ถ์ถ : rating null ์กฐ๊ฑด์ผ๋ก title ์ถ์ถ
toby_not_see = toby_rating.title[toby_rating.rating.isnull()]
print(toby_not_see)
0 Just My
1 Lady
4 The Night
Series -> list
toby_not_see = list(toby_not_see)
3) raw data์์ Toby ๋ฏธ๊ด๋ ์ํ subset ์์ฑ
rating_t = ratings[ratings.title.isin(toby_not_see)]
print(rating_t)
critic title rating
0 Jack Lady 3.0
4 Jack The Night 3.0
5 Mick Lady 3.0
:
30 Gene The Night 3.0
5. Toby ๋ฏธ๊ด๋ ์ํ + Toby ์ ์ฌ๋ join
1) Toby ์ ์ฌ๋ ์ถ์ถ
toby_sim = sim_users[['critic','Toby']] #critic vs Toby ์ ์ฌ๋
2) ํ๊ฐ์ ๊ธฐ์ค ๋ณํฉ
rating_t = pd.merge(rating_t, toby_sim, on='critic')
print(rating_t)
critic title rating Toby
0 Jack Lady 3.0 0.662849
1 Jack The Night 3.0 0.662849
2 Mick Lady 3.0 0.924473
6. ์ํ ์ถ์ฒ ์์ธก
1) ์ ์ฌ๋ ํ์ ๊ณ์ฐ = Toby๋ฏธ๊ด๋ ์ํ ํ์ * Tody ์ ์ฌ๋
rating_t['sim_rating'] = rating_t.rating * rating_t.Toby
print(rating_t)
critic title rating Toby sim_rating
0 Jack Lady 3.0 0.662849 1.988547
1 Jack The Night 3.0 0.662849 1.988547
2 Mick Lady 3.0 0.924473 2.773420
2) ์ํ์ ๋ชฉ๋ณ ํฉ๊ณ
gsum = rating_t.groupby(['title']).sum() #์ํ ์ ๋ชฉ๋ณ ํฉ๊ณ
3) ์ํ์ ๋ชฉ๋ณ ํฉ๊ณ Toby ์ํ์ถ์ฒ ์์ธก = ์ ์ฌ๋ ํ์ / Tody ์ ์ฌ๋
print('\n*** ์ํ ์ถ์ฒ ๊ฒฐ๊ณผ ***')
gsum['predict'] = gsum.sim_rating / gsum.Toby
print(gsum)
rating similarity sim_rating predict
title
Just My 9.5 3.190366 8.074754 2.530981
Lady 11.5 2.959810 8.383808 2.832550
The Night 16.5 3.853215 12.899752 3.347790