license: mit tags: - spam-detection - scikit-learn - multinomial-nb - nlp - english
π‘οΈ SMS & Email Spam Detector
This repository contains a machine learning model designed to classify text messages (SMS or emails) as Spam or Ham (Normal).
π Model Details
- Algorithm: Multinomial Naive Bayes (
MultinomialNB) - Feature Extraction: TF-IDF Vectorizer (
TfidfVectorizer) - Dataset: UCI SMS Spam Collection (5,572 real-world messages)
- Accuracy: ~98% on test data
π§ Deep Dive: Algorithms & Pipeline Architecture
The application relies on a robust Natural Language Processing (NLP) pipeline combined with probabilistic machine learning.
1. Feature Extraction: TF-IDF (TfidfVectorizer)
Raw text cannot be processed directly by mathematical models. The TF-IDF vectorizer converts text messages into weighted numerical matrices:
- Term Frequency (TF): Measures how frequently a word appears in a message.
- Inverse Document Frequency (IDF): Downweights common words (like "the", "is", "to") that appear across all messages and boosts rare, highly discriminative words (like "win", "free", "prize", "click").
2. Classification Algorithm: Multinomial Naive Bayes (MultinomialNB)
- What it is: A probabilistic learning algorithm based on Bayes' Theorem, specifically tuned for text data where features represent word counts or frequencies (multinomial distribution).
- How it works: It calculates the probability of a message belonging to a class (Spam vs. Ham) given the words present in it, assuming independence between word features.
- Why we chose it: It is exceptionally fast, lightweight, highly accurate for text classification tasks, and performs remarkably well with small-to-medium datasets without requiring heavy resources.
π Files Included
spam_model_v2.pkl: The trained Scikit-Learn classification model.tfidf_vectorizer_v2.pkl: The fitted TF-IDF vectorizer for text preprocessing.
π» How to Use
You can load and use the model in your Python project like this:
import joblib
# Load model and vectorizer
model = joblib.load('spam_model_v2.pkl')
vectorizer = joblib.load('tfidf_vectorizer_v2.pkl')
# Test message
messages = ["Congratulations! You've won a free prize. Click here!"]
X_test = vectorizer.transform(messages)
prediction = model.predict(X_test)
print("SPAM" if prediction[0] == 1 else "NORMAL")
Inference Providers NEW
This model isn't deployed by any Inference Provider. π Ask for provider support