การสร้าง document vector แบบ tf-idf

Feature Engineering for NLP in Python

Rounak Banik

Data Scientist

การสร้างโมเดล n-gram

  • น้ำหนักของแต่ละมิติขึ้นอยู่กับความถี่ของคำที่สอดคล้องกัน
    • เอกสารมีคำว่า human ปรากฏอยู่ 5 ครั้ง
    • มิติที่สอดคล้องกับ human มีน้ำหนักเป็น 5
Feature Engineering for NLP in Python

แรงจูงใจ

  • บางคำปรากฏบ่อยมากในเอกสารทุกชิ้น
  • กลุ่มเอกสารเกี่ยวกับจักรวาล
    • เอกสารหนึ่งมีคำว่า jupiter และ universe ปรากฏอยู่ 20 ครั้งเท่ากัน
    • jupiter ปรากฏน้อยในเอกสารอื่น แต่ universe พบบ่อย
    • ให้น้ำหนัก jupiter มากกว่าเพราะมีความเฉพาะเจาะจงสูงกว่า
Feature Engineering for NLP in Python

การประยุกต์ใช้งาน

  • ตรวจจับ stopword โดยอัตโนมัติ
  • การค้นหา
  • ระบบแนะนำ
  • เพิ่มประสิทธิภาพในการสร้างโมเดลเชิงทำนายในบางกรณี
Feature Engineering for NLP in Python

Term frequency-inverse document frequency

  • แปรผันตามความถี่ของคำ (term frequency)
  • แปรผกผันกับจำนวนเอกสารที่คำนั้นปรากฏ
Feature Engineering for NLP in Python

สูตรคณิตศาสตร์

$$\red{w_{i,j}} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$

$$\red{w_{i,j}} \rightarrow \text{น้ำหนักของคำ } i \text{ ในเอกสาร } j$$

Feature Engineering for NLP in Python

สูตรคณิตศาสตร์

$$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 $$

Feature Engineering for NLP in Python

สูตรคณิตศาสตร์

$$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$$

Feature Engineering for NLP in Python

สูตรคณิตศาสตร์

$$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 $

Feature Engineering for NLP in Python

tf-idf ด้วย scikit-learn

# 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

มาฝึกกันเถอะ!

Feature Engineering for NLP in Python

Preparing Video For Download...