Criando um recomendador baseado em enredo

Feature Engineering para NLP em Python

Rounak Banik

Data Scientist

Recomendador de filmes

Título Sinopse
Shanghai Triad Um garoto do interior, parente de uma família do crime em Xangai, é levado pelo tio para a Xangai cosmopolita dos anos 1930 para servir a amante de um chefão.
Cry, the Beloved Country Um pastor sul-africano procura o filho desgarrado que cometeu um crime na cidade grande.
Feature Engineering para NLP em Python

Recomendador de filmes

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 para NLP em Python

Passos

  1. Pré-processar o texto
  2. Gerar vetores tf-idf
  3. Gerar a matriz de similaridade cosseno
Feature Engineering para NLP em Python

A função recomendadora

  1. Receber um título, a matriz de similaridade cosseno e a série de índices como argumentos.
  2. Extrair as pontuações de similaridade cosseno par a par do filme.
  3. Ordenar as pontuações em ordem decrescente.
  4. Exibir os títulos com as maiores pontuações.
  5. Ignorar a maior pontuação de similaridade (1).
Feature Engineering para NLP em Python

Gerando vetores 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 para NLP em Python

Gerando a matriz de similaridade cosseno

# 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 para NLP em Python

A função linear_kernel

  • A magnitude de um vetor tf-idf é 1
  • A similaridade cosseno entre dois vetores tf-idf é seu produto escalar.
  • Pode melhorar bastante o tempo de computação.
  • Use linear_kernel em vez de cosine_similarity.
Feature Engineering para NLP em Python

Gerando a matriz de similaridade cosseno

# 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 para NLP em Python

A função 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 para NLP em Python

Vamos praticar!

Feature Engineering para NLP em Python

Preparing Video For Download...