Créer un système de recommandation basé sur le synopsis

Feature Engineering pour le NLP en Python

Rounak Banik

Data Scientist

Système de recommandation de films

Titre Résumé
Shanghai Triad Un garçon de province, apparenté à une famille du crime de Shanghai, est recruté par son oncle dans le Shanghai des années 1930 pour servir la maîtresse d'un chef de gang.
Cry, the Beloved Country Un pasteur sud-africain part à la recherche de son fils égaré qui a commis un crime en ville.
Feature Engineering pour le NLP en Python

Système de recommandation de films

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 pour le NLP en Python

Étapes

  1. Prétraiter le texte
  2. Générer les vecteurs tf-idf
  3. Générer la matrice de similarité cosinus
Feature Engineering pour le NLP en Python

La fonction de recommandation

  1. Prendre en arguments un titre de film, la matrice de similarité cosinus et la série des index.
  2. Extraire les scores de similarité cosinus par paires pour ce film.
  3. Trier les scores par ordre décroissant.
  4. Renvoyer les titres associés aux meilleurs scores.
  5. Ignorer le score maximal (égal à 1).
Feature Engineering pour le NLP en Python

Générer des vecteurs 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 pour le NLP en Python

Générer la matrice de similarité cosinus

# 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 pour le NLP en Python

La fonction linear_kernel

  • La norme d'un vecteur tf-idf est 1
  • Le score cosinus entre deux vecteurs tf-idf est leur produit scalaire.
  • Peut nettement accélérer le calcul.
  • Utilisez linear_kernel au lieu de cosine_similarity.
Feature Engineering pour le NLP en Python

Générer la matrice de similarité cosinus

# 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 pour le NLP en Python

La fonction 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 pour le NLP en Python

Passons à la pratique !

Feature Engineering pour le NLP en Python

Preparing Video For Download...