콘텐츠 기반 추천 만들기

Python으로 추천 엔진 만들기

Rob O'Callaghan

Director of Data

자카드 유사도 소개

자카드 유사도: 자카드 유사도를 보여 주는 벤 다이어그램. $$J(A,B)=\frac{A\cap B }{A \cup B}$$

Python으로 추천 엔진 만들기

도서 간 자카드 유사도 계산

genres_array_df:

Book Adventure Fantasy Tragedy Social commentary ...
The Hobbit 1 1 0 0 ...
The Great Gatsby 0 0 1 1 ...
A Game of Thrones 0 1 0 0 ...
Macbeth 0 0 1 0 ...
... ... ... ... ... ...
Python으로 추천 엔진 만들기

도서 간 자카드 유사도 계산

from sklearn.metrics import jaccard_score


hobbit_row = book_genre_df.loc['The Hobbit']
GOT_row = book_genre_df.loc['A Game of Thrones']
print(jaccard_score(hobbit_row, GOT_row))
0.5
Python으로 추천 엔진 만들기

모든 항목 간 거리 계산

from scipy.spatial.distance import pdist, squareform


jaccard_distances = pdist(book_genre_df.values, metric='jaccard') print(jaccard_distances)
[1.  0.5 1.  1.  0.5 1. ]
square_jaccard_distances = squareform(jaccard_distances)
print(square_jaccard_distances)
[[0.  1.  0.5 1. ]
 [1.  0.  1.  0.5]
 [0.5 1.  0.  1. ]
 [1.  0.5 1.  0. ]]
Python으로 추천 엔진 만들기

모든 항목 간 거리 계산

print(square_jaccard_distances)
[[0.  1.  0.5 1. ]
 [1.  0.  1.  0.5]
 [0.5 1.  0.  1. ]
 [1.  0.5 1.  0. ]]
jaccard_similarity_array = 1 -  square_jaccard_distances
print(jaccard_similarity_array)
[[1.  0.  0.5 0. ]
 [0.  1.  0.  0.5]
 [0.5 0.  1.  0. ]
 [0.  0.5 0.  1. ]]
Python으로 추천 엔진 만들기

사용 가능한 거리 표 만들기

distance_df = pd.DataFrame(jaccard_similarity_array,
                           index=genres_array_df['Book'], 
                           columns=genres_array_df['Book'])

distance_df.head()
            The Hobbit The Great Gatsby  A Game of Thrones          Macbeth     ...
The Hobbit        1.00             0.15               0.75             0.01     ...
The Great Gatsby  0.15             1.00               0.01             0.43     ...
...
Python으로 추천 엔진 만들기

도서 비교하기

print(distance_df['The Hobbit']['A Game of Thrones'])
0.75
print(distance_df['The Hobbit']['The Great Gatsby'])
0.15
Python으로 추천 엔진 만들기

가장 유사한 도서 찾기

print(distance_df['The Hobbit'].sort_values(ascending=False))
title
The Hobbit                                                             1.00
The Two Towers                                                         0.91
A Game of Thrones                                                      0.50
...
Python으로 추천 엔진 만들기

연습해 봅시다!

Python으로 추천 엔진 만들기

Preparing Video For Download...