Costruire vettori di documenti tf-idf

Feature Engineering per NLP in Python

Rounak Banik

Data Scientist

Modellazione n-gram

  • Peso della dimensione dipende dalla frequenza della parola corrispondente.
    • Il documento contiene la parola human in cinque punti.
    • La dimensione corrispondente a human ha peso 5.
Feature Engineering per NLP in Python

Motivazione

  • Alcune parole compaiono molto spesso in tutti i documenti
  • Corpus di documenti sull'universo
    • Un documento ha jupiter e universe che compaiono 20 volte ciascuna.
    • jupiter è rara negli altri documenti. universe è comune.
    • Dai più peso a jupiter per la sua esclusività.
Feature Engineering per NLP in Python

Applicazioni

  • Riconoscere automaticamente le stopword
  • Ricerca
  • Sistemi di raccomandazione
  • Migliori prestazioni nel modeling predittivo in alcuni casi
Feature Engineering per NLP in Python

Term frequency-inverse document frequency

  • Proporzionale alla frequenza del termine
  • Funzione inversa del numero di documenti in cui compare
Feature Engineering per NLP in Python

Formula matematica

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

$$\red{w_{i,j}} \rightarrow \text{peso del termine } i \text{ nel documento } j$$

Feature Engineering per NLP in Python

Formula matematica

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

$$w_{i,j} \rightarrow \text{peso del termine } i \text{ nel documento } j$$

$$\red{tf_{i,j}} \rightarrow \text{frequenza del termine } i \text{ nel documento } j $$

Feature Engineering per NLP in Python

Formula matematica

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

$$w_{i,j} \rightarrow \text{peso del termine } i \text{ nel documento } j$$

$$tf_{i,j} \rightarrow \text{frequenza del termine } i \text{ nel documento } j $$

$$\red{N} \rightarrow \text{numero di documenti nel corpus} $$

$$\red{df_{i}} \rightarrow \text{numero di documenti che contengono il termine } i$$

Feature Engineering per NLP in Python

Formula matematica

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

$$w_{i,j} \rightarrow \text{peso del termine } i \text{ nel 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$$

Esempio:

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

Feature Engineering per NLP in Python

tf-idf con 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 per NLP in Python

Esercitiamoci!

Feature Engineering per NLP in Python

Preparing Video For Download...