Vytváření tf-idf vektorů dokumentů

Feature Engineering for NLP in Python

Rounak Banik

Data Scientist

Modelování n-gramů

  • Váha dimenze závisí na frekvenci odpovídajícího slova.
    • Dokument obsahuje slovo human na pěti místech.
    • Dimenze odpovídající human má váhu 5.
Feature Engineering for NLP in Python

Motivace

  • Některá slova se vyskytují velmi často ve všech dokumentech.
  • Korpus dokumentů o vesmíru
    • Jeden dokument obsahuje jupiter a universe každé 20×.
    • jupiter se v ostatních dokumentech vyskytuje zřídka, universe často.
    • Přiřadit jupiter vyšší váhu z důvodu exkluzivity.
Feature Engineering for NLP in Python

Aplikace

  • Automatická detekce stopwords
  • Vyhledávání
  • Doporučovací systémy
  • Lepší výkon prediktivního modelování v některých případech
Feature Engineering for NLP in Python

Frekvence termu – inverzní frekvence dokumentu

  • Přímo úměrná frekvenci termu
  • Inverzní funkce počtu dokumentů, ve kterých se vyskytuje
Feature Engineering for NLP in Python

Matematický vzorec

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

$$\red{w_{i,j}} \rightarrow \text{váha termu } i \text{ v dokumentu } j$$

Feature Engineering for NLP in Python

Matematický vzorec

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

$$w_{i,j} \rightarrow \text{váha termu } i \text{ v dokumentu } j$$

$$\red{tf_{i,j}} \rightarrow \text{frekvence termu } i \text{ v dokumentu } j $$

Feature Engineering for NLP in Python

Matematický vzorec

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

$$w_{i,j} \rightarrow \text{váha termu } i \text{ v dokumentu } j$$

$$tf_{i,j} \rightarrow \text{frekvence termu } i \text{v dokumentu } j $$

$$\red{N} \rightarrow \text{počet dokumentů v korpusu} $$

$$\red{df_{i}} \rightarrow \text{počet dokumentů obsahujících term } i$$

Feature Engineering for NLP in Python

Matematický vzorec

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

$$w_{i,j} \rightarrow \text{váha termu } i \text{ v dokumentu } j$$

$$tf_{i,j} \rightarrow frekvence \; termu \; i \; v \; dokumentu \; j $$

$$N \rightarrow po\check{c}et \; dokument\mathring{u} \; v \; korpusu $$

$$df_{i} \rightarrow po\check{c}et \; dokument\mathring{u} \; obsahuj\acute{\imath}c\acute{\imath}ch \; term \; i$$

Příklad:

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

Feature Engineering for NLP in Python

tf-idf pomocí 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 for NLP in Python

Pojďme cvičit!

Feature Engineering for NLP in Python

Preparing Video For Download...