줄거리 기반 추천 시스템 구축

Python으로 배우는 NLP 피처 엔지니어링

Rounak Banik

Data Scientist

영화 추천기

제목 개요
Shanghai Triad 지방 출신 소년이 삼촌 덕에 1930년대 상하이로 와 갱두목의 애인을 보좌하게 된다.
Cry, the Beloved Country 남아공 목사가 대도시에서 범죄를 저지른 아들을 찾으러 간다.
Python으로 배우는 NLP 피처 엔지니어링

영화 추천기

get_recommendations("The Godfather")
1178               The Godfather: Part II
44030    The Godfather Trilogy: 1972-1990
1914              The Godfather: Part III
23126                          Blood Ties
11297                    Household Saints
34717                   Start Liquidation
10821                            Election
38030                          Goodfellas
17729                   Short Sharp Shock
26293                  Beck 28 - Familjen
Name: title, dtype: object
Python으로 배우는 NLP 피처 엔지니어링

단계

  1. 텍스트 전처리
  2. tf-idf 벡터 생성
  3. 코사인 유사도 행렬 생성
Python으로 배우는 NLP 피처 엔지니어링

추천 함수

  1. 영화 제목, 코사인 유사도 행렬, 인덱스 시리즈를 인자로 받습니다.
  2. 해당 영화의 페어와이즈 코사인 점수를 추출합니다.
  3. 점수를 내림차순 정렬합니다.
  4. 상위 점수에 해당하는 제목을 출력합니다.
  5. 최댓값(1)은 제외합니다.
Python으로 배우는 NLP 피처 엔지니어링

tf-idf 벡터 생성

# Import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

# Create TfidfVectorizer object
vectorizer = TfidfVectorizer()

# Generate matrix of tf-idf vectors
tfidf_matrix = vectorizer.fit_transform(movie_plots)
Python으로 배우는 NLP 피처 엔지니어링

코사인 유사도 행렬 생성

# Import cosine_similarity
from sklearn.metrics.pairwise import cosine_similarity

# Generate cosine similarity matrix
cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)
array([[1.        , 0.27435345, 0.23092036, ..., 0.        , 0.        ,
        0.00758112],
       [0.27435345, 1.        , 0.1246955 , ..., 0.        , 0.        ,
        0.00740494],
       ...,
       [0.00758112, 0.00740494, 0.        , ..., 0.        , 0.        ,
        1.        ]])
Python으로 배우는 NLP 피처 엔지니어링

linear_kernel 함수

  • tf-idf 벡터의 크기는 1입니다.
  • 두 tf-idf 벡터의 코사인 점수는 내적입니다.
  • 계산 시간을 크게 줄일 수 있습니다.
  • cosine_similarity 대신 linear_kernel을 사용합니다.
Python으로 배우는 NLP 피처 엔지니어링

코사인 유사도 행렬 생성

# Import cosine_similarity
from sklearn.metrics.pairwise import linear_kernel

# Generate cosine similarity matrix
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
array([[1.        , 0.27435345, 0.23092036, ..., 0.        , 0.        ,
        0.00758112],
       [0.27435345, 1.        , 0.1246955 , ..., 0.        , 0.        ,
        0.00740494],
       ...,
       [0.00758112, 0.00740494, 0.        , ..., 0.        , 0.        ,
        1.        ]])
Python으로 배우는 NLP 피처 엔지니어링

get_recommendations 함수

get_recommendations('The Lion King', cosine_sim, indices)
7782                      African Cats
5877    The Lion King 2: Simba's Pride
4524                         Born Free
2719                          The Bear
4770     Once Upon a Time in China III
7070                        Crows Zero
739                   The Wizard of Oz
8926                   The Jungle Book
1749                 Shadow of a Doubt
7993                      October Baby
Name: title, dtype: object
Python으로 배우는 NLP 피처 엔지니어링

연습해 봅시다!

Python으로 배우는 NLP 피처 엔지니어링

Preparing Video For Download...