0Pricing
NLP Academy · บทเรียน

ฝึกด้วยคุณลักษณะ TF-IDF

ฝึกตัวจำแนกใน scikit-learn

ฝึกด้วยคุณลักษณะ TF-IDF เป็นบทเรียน NLP Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน NLP Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

From Text to Numbers First

A model cannot read raw sentences. You first convert documents into TF-IDF vectors, then train logistic regression on those numbers. 🔢

Fit the Vectorizer

The TfidfVectorizer learns your vocabulary and weighting from the training texts. One call turns a list of strings into a feature matrix.

from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer()
X = vec.fit_transform(train_texts)

Labels Stay Separate

Your features X come from the text, but your labels y come from you. Each document needs its correct class, like spam or not-spam.

Split Before You Train

Hold out a test set so you can judge fairly. train_test_split keeps some data unseen until the very end of evaluation.

from sklearn.model_selection import train_test_split
X_tr, X_te, y_tr, y_te = train_test_split(X, y)

Fit Means Learn

Calling fit tells logistic regression to find the word weights that best separate your classes on the training data.

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(max_iter=1000)
clf.fit(X_tr, y_tr)

Raise max_iter If It Warns

Text has many features, so the solver may need more steps. Bumping max_iter clears the common convergence warning you will see.

Predict on New Vectors

To classify fresh text, transform it with the same fitted vectorizer, then call predict. Never refit the vectorizer on test data.

preds = clf.predict(X_te)

Transform, Do Not Fit, on Test

Test text uses transform, not fit_transform. Refitting would leak test information and quietly inflate your scores.

Check the Accuracy

A quick score call gives accuracy on the held-out set. It is your first signal that training actually worked.

print(clf.score(X_te, y_te))

Same Steps Stay in Sync

Train and prediction must share the exact same vocabulary. Using one fitted vectorizer everywhere keeps feature columns aligned.

A Pipeline Saves You

Wrapping the vectorizer and model in a Pipeline chains transform and predict automatically, so you never forget a step.

from sklearn.pipeline import make_pipeline
pipe = make_pipeline(TfidfVectorizer(), LogisticRegression())

Quick Check

How should you prepare test text before predicting?

Recap: Vectorize Then Fit

Vectorize text with TF-IDF, split, then fit logistic regression. Reuse the same vectorizer for test data, ideally inside a pipeline. ✅

คำถามที่พบบ่อย

บทเรียน “ฝึกด้วยคุณลักษณะ TF-IDF” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ฝึกด้วยคุณลักษณะ TF-IDF” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส NLP Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ฝึกด้วยคุณลักษณะ TF-IDF”

ฝึกตัวจำแนกใน scikit-learn คุณปฏิบัติ NLP Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน NLP Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน NLP Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ฝึกด้วยคุณลักษณะ TF-IDF” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน NLP Academy นี้ได้ไหม

ได้ บทเรียน NLP Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใด Logistic Regression จึงชนะกับข้อมูลข้อความ
  2. ฝึกด้วยคุณลักษณะ TF-IDF
  3. ตรวจสอบสัมประสิทธิ์ที่มีอิทธิพลสูงสุด
  4. ปรับความแรงของการทำให้เป็นปกติ
← กลับไปที่ NLP Academy