Construindo vetores de documentos com tf-idf

Feature Engineering para NLP em Python

Rounak Banik

Data Scientist

Modelagem com n-grams

  • Peso da dimensão depende da frequência da palavra dessa dimensão.
    • O documento contém a palavra human cinco vezes.
    • A dimensão de human tem peso 5.
Feature Engineering para NLP em Python

Motivação

  • Algumas palavras ocorrem muito em todos os documentos
  • Corpus de documentos sobre o universo
    • Um documento tem jupiter e universe ocorrendo 20 vezes cada.
    • jupiter raramente aparece nos outros documentos. universe é comum.
    • Dê mais peso a jupiter por exclusividade.
Feature Engineering para NLP em Python

Aplicações

  • Detectar stopwords automaticamente
  • Busca
  • Sistemas de recomendação
  • Melhor desempenho em alguns modelos preditivos
Feature Engineering para NLP em Python

Term frequency-inverse document frequency

  • Proporcional à frequência do termo
  • Função inversa do número de documentos em que ocorre
Feature Engineering para NLP em Python

Fórmula matemática

$$\red{w_{i,j}} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$

$$\red{w_{i,j}} \rightarrow \text{peso do termo } i \text{ no documento } j$$

Feature Engineering para NLP em Python

Fórmula matemática

$$w_{i,j} = \red{tf_{i,j}} \cdot \log\left(\frac{N}{df_{i}}\right) $$

$$w_{i,j} \rightarrow \text{peso do termo } i \text{ no documento } j$$

$$\red{tf_{i,j}} \rightarrow \text{frequência do termo } i \text{ no documento } j $$

Feature Engineering para NLP em Python

Fórmula matemática

$$w_{i,j} = tf_{i,j} \cdot \red{\log\left(\frac{N}{df_{i}}\right)} $$

$$w_{i,j} \rightarrow \text{peso do termo } i \text{ no documento } j$$

$$tf_{i,j} \rightarrow \text{frequência do termo } i \text{no documento } j $$

$$\red{N} \rightarrow \text{número de documentos no corpus } $$

$$\red{df_{i}} \rightarrow \text{número de documentos que contêm o termo } i$$

Feature Engineering para NLP em Python

Fórmula matemática

$$w_{i,j} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$

$$w_{i,j} \rightarrow \text{peso do termo } i \text{ no documento } j$$

$$tf_{i,j} \rightarrow term \; frequency \; of \; term \; i \; in \; document \; j $$

$$N \rightarrow number \; of \; documents \; in \; the \; corpus $$

$$df_{i} \rightarrow number \; of \; documents \; cotaining \; term \; i$$

Example:

$\red{w_{library, document}} = 5 \cdot log(\frac{20}{8}) \approx 2 $

Feature Engineering para NLP em Python

tf-idf com scikit-learn

# Import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

# Create TfidfVectorizer object vectorizer = TfidfVectorizer()
# Generate matrix of word vectors tfidf_matrix = vectorizer.fit_transform(corpus) print(tfidf_matrix.toarray())
[[0.         0.         0.         0.         0.25434658 0.33443519
  0.33443519 0.         0.25434658 0.         0.25434658 0.
  0.76303975]
 [0.         0.46735098 0.         0.46735098 0.         0.
  0.         0.46735098 0.         0.46735098 0.35543247 0.
  0.        ]
...
Feature Engineering para NLP em Python

Vamos praticar!

Feature Engineering para NLP em Python

Preparing Video For Download...