Creare un recommender basato sulla trama

Feature Engineering per NLP in Python

Rounak Banik

Data Scientist

Recommender di film

Titolo Trama
Shanghai Triad Un ragazzo di provincia, parente di una famiglia criminale di Shanghai, viene portato dallo zio nella Shanghai cosmopolita degli anni '30 per servire l'amante di un boss.
Cry, the Beloved Country Un pastore sudafricano cerca il figlio scapestrato che ha commesso un crimine nella grande città.
Feature Engineering per NLP in Python

Recommender di 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
Feature Engineering per NLP in Python

Passi

  1. Pre-elaborazione del testo
  2. Genera vettori tf-idf
  3. Genera matrice di similarità coseno
Feature Engineering per NLP in Python

La funzione di raccomandazione

  1. Accetta titolo del film, matrice di similarità coseno e serie degli indici come argomenti.
  2. Estrai gli score di similarità coseno per il film.
  3. Ordina gli score in ordine decrescente.
  4. Restituisci i titoli con gli score più alti.
  5. Ignora lo score più alto (pari a 1).
Feature Engineering per NLP in Python

Generare vettori 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)
Feature Engineering per NLP in Python

Generare la matrice di similarità coseno

# 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.        ]])
Feature Engineering per NLP in Python

La funzione linear_kernel

  • La magnitudine di un vettore tf-idf è 1
  • Il punteggio coseno tra due vettori tf-idf è il loro prodotto scalare.
  • Può migliorare molto i tempi di calcolo.
  • Usa linear_kernel invece di cosine_similarity.
Feature Engineering per NLP in Python

Generare la matrice di similarità coseno

# 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.        ]])
Feature Engineering per NLP in Python

La funzione 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
Feature Engineering per NLP in Python

Esercitiamoci!

Feature Engineering per NLP in Python

Preparing Video For Download...