构建垃圾信息检测器
使用带标签的消息进行训练
构建垃圾信息检测器 是 CoddyKit 上的免费 NLP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 NLP Academy 课程的其余内容,请升级到 CoddyKit PRO。 NLP Academy 课程共包含 4 节课。
「构建垃圾信息检测器」这节课中我会学到什么?
使用带标签的消息进行训练 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NLP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NLP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「构建垃圾信息检测器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NLP Academy 课中编写并运行代码吗?
能。每节 NLP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 朴素贝叶斯背后的直觉
- 构建垃圾信息检测器
- 多项式模型与伯努利模型
- 解读模型的预测结果