Your First Machine Learning Model: A Scam SMS Detector in Python
Build a real machine learning model with scikit-learn that tells scam mobile money messages from genuine ones, and learn what each step does.

Machine learning can sound like something only big tech companies do. But the core idea fits in a few lines of Python: show the computer examples, let it find the pattern, then ask it about something new. In this guide you'll build a real (tiny) machine learning model that tells scam mobile money messages from genuine ones.
It's a fitting first project: MTN Ghana has reported using AI to detect and block fraudulent mobile money messages. Yours will be far simpler, but it works on the same principle.
What you need
Python 3 on your computer (see How to Install Python on Your Computer) and the scikit-learn library. In a terminal, inside your project's virtual environment, run:
python -m pip install scikit-learn
The idea: learn from labelled examples
We'll give the model eight messages, each labelled genuine or scam. It will learn which words tend to appear in each kind, then label two messages it has never seen. This is called supervised learning: learning from examples that come with the right answers.
The code
Save this as scam_sms.py:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
messages = [
"You have received GHS 50.00 from Ama Mensah. Your new balance is GHS 120.00",
"Payment of GHS 15.00 to Kofi's Shop was successful. Balance GHS 85.00",
"You have received GHS 200.00 from Yaw Boateng. Your new balance is GHS 310.00",
"Your airtime purchase of GHS 5.00 was successful",
"Congratulations! You have won GHS 5,000. Send your PIN to claim your prize now",
"URGENT: your MoMo account is blocked. Call this number and give your PIN to unlock",
"You received GHS 900 by mistake. Please send it back quickly to this number",
"Win big today! Send GHS 20 to register and receive GHS 2,000 cash prize",
]
labels = ["genuine"] * 4 + ["scam"] * 4
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(messages)
model = MultinomialNB().fit(X, labels)
new = ["You have won a prize! Send your PIN to claim GHS 1,000",
"You have received GHS 30.00 from Esi Owusu. Your new balance is GHS 75.00"]
print(model.predict(vectorizer.transform(new)).tolist())
Run it with python scam_sms.py:
Output:
['scam', 'genuine']
The model labelled the prize message a scam and the transfer receipt genuine, even though neither was in its examples.
What just happened
1. Turning words into numbers
Models work with numbers, not text. CountVectorizer builds a vocabulary of every word in the messages, then turns each message into a row of counts: how many times "pin", "won", "received", "balance" and so on appear. This simple trick is called a bag of words.
2. Learning the pattern
MultinomialNB is a Naive Bayes classifier, a classic and very fast model for text. During fit, it works out how likely each word is in scam messages and in genuine ones. "Pin", "won" and "prize" turn up in scams; "received" and "balance" turn up in genuine receipts.
3. Predicting
For a new message, the model counts its words the same way and asks which label makes those words most likely. "Won", "prize" and "PIN" point strongly to scam, so that's its answer.
Be honest about what it can do
Eight examples is a toy. A real fraud filter learns from many thousands of messages and is tested on messages it never saw during training, to measure how often it's right and, just as important, how often it wrongly blocks a genuine message. Scammers also change their wording, so real systems keep learning. Those ideas (train/test splits, accuracy that misleads, false alarms versus missed scams) are exactly what a machine learning course teaches next.
Try it yourself
- Add your own message to
new, such as a fake "your account will be closed" warning, and see what the model says. - Add more genuine and scam examples to
messages(and their labels) and watch the predictions get more reliable. - Print
vectorizer.get_feature_names_out()to see the vocabulary the model learned from.
Learn it properly
- Machine Learning with scikit-learn: the full workflow, from splitting data and choosing models to measuring them honestly and avoiding common traps.
- NLP and Generative AI: Building with Language Models: text classification, embeddings and modern language models.
- New to Python or data? Start with the Python Crash Course and Data Analysis with NumPy and pandas.
The AI and Machine Learning Engineer roadmap shows where this fits on the path to building AI.
Comments
Log in to join the conversation.
Loading comments...


