プロットに基づくレコメンダーの構築

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. 映画タイトル、コサイン類似度行列、インデックス Series を引数に取る。
  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。
  • 2 つの 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の特徴量エンジニアリング

Passons à la pratique !

Pythonで学ぶNLPの特徴量エンジニアリング

Preparing Video For Download...