📘 Lesson · Lesson 94
Email Spam Detection
About this Project
💡 At a Glance
This project classifies messages as spam or not spam using a Naive Bayes model on text.
The Program
Python
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
texts = ["win money now", "meeting at office", "free lottery prize", "project deadline today"]
labels = ["spam", "ham", "spam", "ham"]
vec = CountVectorizer()
X = vec.fit_transform(texts)
model = MultinomialNB()
model.fit(X, labels)
test = vec.transform(["win free prize"])
print("Prediction:", model.predict(test)[0])Prediction: spam
How it Works
- CountVectorizer turns text into word-count numbers.
- Naive Bayes learns which words appear in spam vs ham, then predicts.
Summary
- Convert text to numbers, train Naive Bayes, then predict spam/ham.
- A practical introduction to text classification.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.