Feature Engineering for NLP in Python
Rounak Banik
Data Scientist
human ปรากฏอยู่ 5 ครั้งhuman มีน้ำหนักเป็น 5jupiter และ 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$$
$$w_{i,j} = \red{tf_{i,j}} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$w_{i,j} \rightarrow \text{น้ำหนักของคำ } i \text{ ในเอกสาร } j$$
$$\red{tf_{i,j}} \rightarrow \text{ความถี่ของคำ } i \text{ ในเอกสาร } j $$
$$w_{i,j} = tf_{i,j} \cdot \red{\log\left(\frac{N}{df_{i}}\right)} $$
$$w_{i,j} \rightarrow \text{น้ำหนักของคำ } i \text{ ในเอกสาร } j$$
$$tf_{i,j} \rightarrow \text{ความถี่ของคำ } i \text{ในเอกสาร } j $$
$$\red{N} \rightarrow \text{จำนวนเอกสารทั้งหมดใน corpus} $$
$$\red{df_{i}} \rightarrow \text{จำนวนเอกสารที่มีคำ } i$$
$$w_{i,j} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$
$$w_{i,j} \rightarrow \text{น้ำหนักของคำ } i \text{ ในเอกสาร } j$$
$$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. ]
...
Feature Engineering for NLP in Python