Membangun recommender berbasis alur cerita

Rekayasa Fitur untuk NLP di Python

Rounak Banik

Data Scientist

Rekomendasi film

Judul Ringkasan
Shanghai Triad Seorang anak desa yang terkait keluarga kejahatan Shanghai direkrut pamannya ke Shanghai kosmopolitan 1930-an untuk menjadi pelayan gundik bos geng.
Cry, the Beloved Country Seorang pendeta Afrika Selatan mencari putranya yang tersesat yang melakukan kejahatan di kota besar.
Rekayasa Fitur untuk NLP di Python

Rekomendasi film

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
Rekayasa Fitur untuk NLP di Python

Langkah-langkah

  1. Pra-pemrosesan teks
  2. Buat vektor tf-idf
  3. Buat matriks kemiripan kosinus
Rekayasa Fitur untuk NLP di Python

Fungsi recommender

  1. Ambil judul film, matriks kemiripan kosinus, dan seri indeks sebagai argumen.
  2. Ambil skor kemiripan kosinus berpasangan untuk film itu.
  3. Urutkan skor menurun.
  4. Keluarkan judul dengan skor tertinggi.
  5. Abaikan skor tertinggi (= 1).
Rekayasa Fitur untuk NLP di Python

Membuat vektor 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)
Rekayasa Fitur untuk NLP di Python

Membuat matriks kemiripan kosinus

# 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.        ]])
Rekayasa Fitur untuk NLP di Python

Fungsi linear_kernel

  • Magnitudo vektor tf-idf adalah 1
  • Skor kosinus antar vektor tf-idf = produk titiknya.
  • Dapat mempercepat komputasi secara signifikan.
  • Gunakan linear_kernel alih-alih cosine_similarity.
Rekayasa Fitur untuk NLP di Python

Membuat matriks kemiripan kosinus

# 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.        ]])
Rekayasa Fitur untuk NLP di Python

Fungsi 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
Rekayasa Fitur untuk NLP di Python

Ayo berlatih!

Rekayasa Fitur untuk NLP di Python

Preparing Video For Download...