Feature Engineering for NLP in Python
Rounak Banik
Data Scientist
human na pěti místech.human má váhu 5.jupiter a universe každé 20×.jupiter se v ostatních dokumentech vyskytuje zřídka, universe často.jupiter vyšší váhu z důvodu exkluzivity.$$\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$$
$$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 $$
$$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$$
$$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 $
# 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