Feature Engineering voor NLP in Python
Rounak Banik
Data Scientist
human vijf keer.human heeft gewicht 5.jupiter en universe elk 20 keer.jupiter komt zelden in andere documenten voor. universe is algemeen.jupiter meer gewicht vanwege exclusiviteit.$$\red{w_{i,j}} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$\red{w_{i,j}} \rightarrow \text{gewicht van term } i \text{ in document } j$$
$$w_{i,j} = \red{tf_{i,j}} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$w_{i,j} \rightarrow \text{gewicht van term } i \text{ in document } j$$
$$\red{tf_{i,j}} \rightarrow \text{termfrequentie van term } i \text{ in document } j $$
$$w_{i,j} = tf_{i,j} \cdot \red{\log\left(\frac{N}{df_{i}}\right)} $$
$$w_{i,j} \rightarrow \text{gewicht van term } i \text{ in document } j$$
$$tf_{i,j} \rightarrow \text{termfrequentie van term } i \text{ in document } j $$
$$\red{N} \rightarrow \text{aantal documenten in het corpus } $$
$$\red{df_{i}} \rightarrow \text{aantal documenten met term } i$$
$$w_{i,j} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$w_{i,j} \rightarrow \text{gewicht van term } i \text{ in document } j$$
$$tf_{i,j} \rightarrow term\;frequentie\;van\;term\;i\;in\;document\;j $$
$$N \rightarrow aantal\;documenten\;in\;het\;corpus $$
$$df_{i} \rightarrow aantal\;documenten\;met\;term\;i$$
Voorbeeld:
$\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 voor NLP in Python