สร้างตัวตรวจจับสแปม
ฝึกด้วยข้อความที่มีป้ายกำกับ
สร้างตัวตรวจจับสแปม เป็นบทเรียน NLP Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน NLP Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Your First Real Model
Time to build something useful: a spam filter. You will train a classifier on labeled messages and let it judge brand-new ones.
Start With Labeled Data
Every supervised model needs examples with answers. Here each message comes with a label of spam or ham, the friendly name for not-spam.
messages = ["win cash now", "lunch at noon?", "free prize click"]
labels = ["spam", "ham", "spam"]
print(len(messages), len(labels))Turn Text Into Numbers
The model cannot read raw words, so you count them first. A CountVectorizer converts each message into a row of word counts.
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
X = vec.fit_transform(messages)Meet MultinomialNB
For word counts, the right tool is MultinomialNB, the count-based flavor of Naive Bayes built right into scikit-learn.
from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB()Fit the Model
Training is one line: hand the model your features and labels. The fit call counts words per class and stores the probabilities.
model.fit(X, labels)
print("trained on", X.shape[0], "messages")Predict New Mail
To judge a fresh message, vectorize it the same way and call predict. The model returns its best guess for spam or ham.
new = vec.transform(["free cash prize"])
print(model.predict(new))Reuse the Vectorizer
New text must use the same vocabulary the model learned. Always call transform, never fit again, or the columns will not line up.
Peek at the Confidence
Beyond the label, the model can report how sure it is. The predict_proba method gives a probability for each possible class.
print(model.predict_proba(new))Hold Out a Test Set
Never grade a model on the data it trained on. Split off a test set so you can measure how it does on unseen messages.
Score the Filter
Run predictions on the held-out messages and compare to the truth. That accuracy tells you whether your spam filter actually works. 📬
Iterate to Improve
More clean data and better preprocessing lift results fast. Treat this filter as a baseline you can steadily refine, not a finished product.
Quick Check
How should you prepare a new message before predicting on it?
Recap
You vectorized messages, trained MultinomialNB, and predicted spam on new text. Reusing the fitted vectorizer keeps your features aligned. ✅
คำถามที่พบบ่อย
บทเรียน “สร้างตัวตรวจจับสแปม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สร้างตัวตรวจจับสแปม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส NLP Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สร้างตัวตรวจจับสแปม”
ฝึกด้วยข้อความที่มีป้ายกำกับ คุณปฏิบัติ NLP Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน NLP Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน NLP Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “สร้างตัวตรวจจับสแปม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน NLP Academy นี้ได้ไหม
ได้ บทเรียน NLP Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- สัญชาตญาณเบื้องหลัง Naive Bayes
- สร้างตัวตรวจจับสแปม
- โมเดลพหุนามเทียบกับเบอร์นูลลี
- อ่านผลการคาดการณ์ของโมเดล