A Machine Learning project for detecting Spam and Ham messages using TF-IDF and Logistic Regression.
This project uses Natural Language Processing (NLP) and Machine Learning to classify messages into two categories:
- Ham — Normal / legitimate messages
- Spam — Unwanted or suspicious messages
Text Data
↓
Data Cleaning
↓
Label Encoding
↓
TF-IDF Feature Extraction
↓
Train/Test Split
↓
Logistic Regression
↓
Prediction
↓
Model Evaluation
The dataset contains 6,464 messages with two columns:
| Column | Description |
|---|---|
label |
Target category: spam or ham |
text |
Message content |
| label | text |
|---|---|
| ham | Hello, how are you? |
| spam | Congratulations! You won a free prize! |
- Python
- Pandas
- NumPy
- Scikit-learn
- TF-IDF
- Matplotlib
- Seaborn
- Joblib
- Flask (optional for deployment)
import pandas as pd
email = pd.read_csv("after data cleaning email.csv")email["text"] = email["text"].fillna("empty message")from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
encoded = encoder.fit_transform(email["label"])from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()
X_tfidf = vectorizer.fit_transform(email["text"])from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X_tfidf,
encoded,
test_size=0.2,
random_state=42,
stratify=encoded
)from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)predictions = model.predict(X_test)Prediction probability:
probabilities = model.predict_proba(X_test)from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)After training, save both the trained model and TF-IDF vectorizer:
import joblib
joblib.dump(model, "spam_model.pkl")
joblib.dump(vectorizer, "tfidf_vectorizer.pkl")spam_model.pkl
tfidf_vectorizer.pkl
This allows the trained model to be reused without training it again.
message = ["Congratulations! You have won a free prize"]
message_tfidf = vectorizer.transform(message)
prediction = model.predict(message_tfidf)
print(prediction)- Clean and preprocess text data
- Encode categorical labels
- Apply TF-IDF feature engineering
- Split data into training and testing sets
- Train a Logistic Regression classifier
- Predict Spam and Ham messages
- Calculate model accuracy
- Save the trained model
- Save the TF-IDF vectorizer
- Prepare the model for deployment
- Data Cleaning
- Label Encoding
- Natural Language Processing
- TF-IDF
- Feature Engineering
- Train/Test Split
- Supervised Learning
- Logistic Regression
- Classification
- Model Training
- Prediction
- Prediction Probability
- Accuracy Score
- Model Saving with Joblib
- Build a Flask web application
- Add a message input form
- Display prediction probability
- Improve text preprocessing
- Compare different classification algorithms
- Add confusion matrix and classification report
- Deploy the application online
Clone the repository:
git clone https://github.com/rabia743/Email-Spam-detector-ML-model.git
cd Email-Spam-detector-ML-modelInstall required libraries:
pip install pandas numpy scikit-learn matplotlib seaborn scipy joblibThe same TF-IDF vectorizer used during training must be reused during prediction.
Training:
Text
↓
TF-IDF Vectorizer
↓
Machine Learning Model
Prediction:
New Text
↓
Same TF-IDF Vectorizer
↓
Saved Model
↓
Prediction
Rabia Zafar
Python Developer | Machine Learning Developer
Built with Python, NLP & Machine Learning
