TF-IDF ด้วย scikit-learn
แปลงคลังเอกสารเป็นเวกเตอร์ด้วยโค้ดไม่กี่บรรทัด
TF-IDF ด้วย scikit-learn เป็นบทเรียน NLP Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน NLP Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
No Need to Hand-Code It
You understand the math, so now let scikit-learn do the heavy lifting. Its TfidfVectorizer turns raw documents into a weighted matrix in a few lines.
Import the Vectorizer
Everything lives in the feature_extraction.text module. Import TfidfVectorizer and you are ready to vectorize any list of text strings.
from sklearn.feature_extraction.text import TfidfVectorizerYour Corpus Is a List
A corpus is just a Python list of strings, one per document. Each entry is the full text you want scored and compared.
corpus = [
"the cat sat on the mat",
"the dog chased the cat",
]Fit and Transform
Call fit_transform to learn the vocabulary and compute TF-IDF in one step. It returns a sparse matrix of weighted features.
vec = TfidfVectorizer()
X = vec.fit_transform(corpus)
print(X.shape)What fit Learned
The fit step builds the vocabulary and the IDF values from your corpus. After this the vectorizer knows every term and how rare it is.
Inspect the Vocabulary
You can list the learned feature names to see the columns. get_feature_names_out shows each word in vocabulary order. 🔎
print(vec.get_feature_names_out())The Output Is Sparse
Most documents use only a few words, so the matrix is mostly zeros. scikit-learn stores it as a memory-saving sparse matrix by default.
Peek at Real Numbers
Convert a row to a dense array to actually read the weights. The fillers near zero and topic words stand out clearly.
print(X.toarray()[0].round(3))Tune With Parameters
Handy options let you drop rare or common terms instantly. Set min_df and stop_words to clean the vocabulary as you vectorize.
vec = TfidfVectorizer(stop_words="english", min_df=2)Reuse on New Text
Fit once on training data, then call transform on fresh documents. New text is mapped into the exact same vocabulary and IDF scale.
new_docs = ["a new cat appeared"]
X_new = vec.transform(new_docs)Ready for a Model
This weighted matrix plugs straight into any scikit-learn classifier. TF-IDF features are a strong, fast baseline for real text tasks.
Quick Check
Which method learns the vocabulary and computes the TF-IDF matrix together?
Recap
You imported TfidfVectorizer, fit it on a corpus, inspected the sparse output, and learned to reuse it on new text. The math is now a one-liner. ✅
คำถามที่พบบ่อย
บทเรียน “TF-IDF ด้วย scikit-learn” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “TF-IDF ด้วย scikit-learn” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส NLP Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “TF-IDF ด้วย scikit-learn”
แปลงคลังเอกสารเป็นเวกเตอร์ด้วยโค้ดไม่กี่บรรทัด คุณปฏิบัติ NLP Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน NLP Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน NLP Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “TF-IDF ด้วย scikit-learn” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน NLP Academy นี้ได้ไหม
ได้ บทเรียน NLP Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ปัญหาของการนับแบบดิบ
- ความถี่ของคำและความถี่ผกผันของเอกสาร
- TF-IDF ด้วย scikit-learn
- ค้นหาคำที่สำคัญที่สุด