๐ก๏ธ An intelligent Machine Learning application that classifies emails as Spam or Ham (Not Spam) using Natural Language Processing and Logistic Regression.
- Overview
- Demo
- Features
- How It Works
- Dataset
- Model Pipeline
- Installation
- Usage
- Model Performance
- Project Structure
- Technologies Used
- Contributing
- License
Spam emails are a constant nuisance, cluttering inboxes and sometimes posing security threats. This project implements a Machine Learning-based Spam Classifier that can automatically detect and filter spam emails with high accuracy.
- ๐ฌ Email Overload: Over 45% of all emails sent daily are spam
- ๐ Security: Spam often contains phishing attempts and malware
- โฑ๏ธ Time Saving: Automated filtering saves hours of manual sorting
- ๐ค AI-Powered: Uses NLP techniques for intelligent classification
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ง Spam Email Classifier โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ Enter your email message here: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Congratulations! You've won $1,000,000! Click here โ โ
โ โ to claim your prize now. Limited time offer!!! โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ [ ๐ Predict ] โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ ๐ซ Spam Email Detected! โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Feature | Description |
|---|---|
| ๐ค Text Processing | TF-IDF vectorization for feature extraction |
| ๐งน Data Cleaning | Removes duplicates and handles null values |
| โ๏ธ Balanced Dataset | Handles class imbalance through sampling |
| ๐ Web Interface | Interactive Streamlit app for real-time predictions |
| ๐ Visualization | Decision boundary plots using PCA |
| ๐พ Model Persistence | Saved model for easy deployment |
โโโโโโโโโโโโโโโโโโโโ
โ Input Email โ
โ Message โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Text Preprocessing โ
โ โข Lowercase conversion โ
โ โข Stop words removal โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TF-IDF Vectorization โ
โ Transform text to โ
โ numerical features โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Logistic Regression โ
โ Classification โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโ
โ โ
โผ โผ
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ ๐ซ SPAM โ โ โ
HAM โ
โ (Class 0) โ โ (Class 1) โ
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
The model is trained on a mail dataset containing email messages labeled as spam or ham.
- Load Data - Read CSV file with email messages
- Handle Nulls - Check and manage missing values
- Remove Duplicates - Clean duplicate entries
- Label Encoding - Convert categories (Spam=0, Ham=1)
- Balance Classes - Sample to handle class imbalance
| Category | Label | Description |
|---|---|---|
| Ham | 1 | Legitimate email (Not Spam) |
| Spam | 0 | Unwanted/Junk email |
TfidfVectorizer(
min_df=1, # Minimum document frequency
stop_words='english', # Remove common English words
lowercase=True # Convert to lowercase
)What is TF-IDF?
- TF (Term Frequency): How often a word appears in a document
- IDF (Inverse Document Frequency): How important a word is across all documents
- TF-IDF: Combines both to give weight to important words
A linear classifier that predicts the probability of an email being spam or ham based on the TF-IDF features.
- Python 3.8 or higher
- pip package manager
-
Clone the repository
git clone https://github.com/Lakshya2031/Spam_mail_detector_2.git cd Spam_mail_detector_2 -
Create virtual environment (recommended)
python -m venv venv # Windows venv\Scripts\activate # macOS/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
streamlit run "app_py (4).py"Access the app at http://localhost:8501
Option 1: Open in Google Colab (Recommended)
- Click the "Open in Colab" badge at the top
Option 2: Run locally
jupyter notebook Mail_Detection.ipynbimport pickle
# Load model and vectorizer
model = pickle.load(open('spam_model1.pkl', 'rb'))
vectorizer = pickle.load(open('feature_extraction1.pkl', 'rb'))
# Test message
message = "Congratulations! You won a free iPhone! Click here now!"
prediction = model.predict(vectorizer.transform([message]))
print("Spam" if prediction[0] == 0 else "Ham")| Metric | Value |
|---|---|
| Algorithm | Logistic Regression |
| Feature Extraction | TF-IDF Vectorizer |
| Test Accuracy | ~96% |
| Train-Test Split | 80-20 |
| Random State | 3 |
The notebook includes a Decision Boundary Plot using PCA dimensionality reduction to visualize how the model separates spam from ham emails.
Spam_mail_detector_2/
โ
โโโ ๐ Mail_Detection.ipynb # Jupyter notebook with full analysis
โโโ ๐ app_py (4).py # Streamlit web application
โโโ ๐ฆ spam_model1.pkl # Trained Logistic Regression model
โโโ ๐ฆ feature_extraction1.pkl # Fitted TF-IDF Vectorizer
โโโ ๐ requirements.txt # Python dependencies
โโโ ๐ readme.md # Project documentation
| Category | Technology | Purpose |
|---|---|---|
| Language | Python 3.8+ | Core programming |
| Data Processing | Pandas, NumPy | Data manipulation |
| NLP | TF-IDF Vectorizer | Text feature extraction |
| Machine Learning | Scikit-learn | Model training & evaluation |
| Visualization | Matplotlib, mlxtend | Plotting decision boundaries |
| Web Framework | Streamlit | Interactive web app |
| Serialization | Pickle | Model persistence |
- Add more ML algorithms (Naive Bayes, SVM, Random Forest)
- Implement deep learning with LSTM/Transformers
- Add email header analysis
- Create browser extension
- Deploy on cloud (Heroku/AWS/GCP)
- Add batch email processing
- Implement model retraining pipeline
Contributions make the open-source community amazing! Here's how to contribute:
- Fork the repository
- Create your feature branch
git checkout -b feature/AmazingFeature
- Commit your changes
git commit -m 'Add some AmazingFeature' - Push to the branch
git push origin feature/AmazingFeature
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Lakshya
If you found this project helpful, please consider giving it a โญ on GitHub!
Have questions or suggestions? Feel free to:
- Open an Issue
- Submit a Pull Request
๐ก๏ธ Stay Safe from Spam with AI! ๐ก๏ธ
Made with โค๏ธ using Machine Learning