Python 中文本特徵工程
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 中文本特徵工程