Building a Spam Detector
Train on labeled messages.
Building a Spam Detector is a free NLP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. ✅
Frequently asked questions
Is the “Building a Spam Detector” lesson free?
Yes — the full text of “Building a Spam Detector” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building a Spam Detector”?
Train on labeled messages. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Spam Detector” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Intuition Behind Naive Bayes
- Building a Spam Detector
- Multinomial vs Bernoulli Models
- Reading the Model's Predictions