Python 中的 NLP 特征工程
Rounak Banik
Data Scientist
human 出现 5 次。human 对应的维度权重为 5。jupiter 与 universe 各出现 20 次。jupiter 在其他文档中很少见,universe 很常见。jupiter 赋予更高权重。$$\red{w_{i,j}} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$\red{w_{i,j}} \rightarrow \text{术语 } i \text{ 在文档 } j \text{ 中的权重}$$
$$w_{i,j} = \red{tf_{i,j}} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$w_{i,j} \rightarrow \text{术语 } i \text{ 在文档 } j \text{ 中的权重}$$
$$\red{tf_{i,j}} \rightarrow \text{术语 } i \text{ 在文档 } j \text{ 中的出现频率} $$
$$w_{i,j} = tf_{i,j} \cdot \red{\log\left(\frac{N}{df_{i}}\right)} $$
$$w_{i,j} \rightarrow \text{术语 } i \text{ 在文档 } j \text{ 中的权重}$$
$$tf_{i,j} \rightarrow \text{术语 } i \text{ 在文档 } j \text{ 中的出现频率} $$
$$\red{N} \rightarrow \text{语料库中的文档数} $$
$$\red{df_{i}} \rightarrow \text{包含术语 } i \text{ 的文档数}$$
$$w_{i,j} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$w_{i,j} \rightarrow \text{术语 } i \text{ 在文档 } j \text{ 中的权重}$$
$$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$$
示例:
$\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. ]
...
Python 中的 NLP 特征工程