A machine learning project that classifies emails as spam or legitimate (ham) using Logistic Regression and TF-IDF feature extraction.
This project builds a predictive model to automatically detect and classify spam emails. It uses natural language processing techniques combined with a Logistic Regression classifier to achieve high accuracy in spam detection.
- Data Preprocessing: Handles missing values and cleans email data
- Feature Extraction: Utilizes TF-IDF (Term Frequency-Inverse Document Frequency) vectorization
- Machine Learning: Implements Logistic Regression for binary classification
- Model Evaluation: Provides accuracy metrics on both training and test datasets
- Predictive System: Makes real-time predictions on new email inputs
The project uses a mail classification dataset (mail_data.csv) containing:
- Message: The email content
- Category: Classification label (spam/ham)
The dataset includes emails labeled as either:
spam(0): Unsolicited or malicious emailsham(1): Legitimate emails
Spam Mail Predictor/
├── Spam Mail Predictor.ipynb # Main Jupyter notebook
├── Dataset/
│ └── mail_data.csv # Email classification dataset
└── README.md # Project documentation
numpy
pandas
scikit-learn
- Clone the repository:
git clone <repository-url>
cd Spam\ Mail\ Predictor- Install the required dependencies:
pip install numpy pandas scikit-learn- Ensure you have Jupyter Notebook installed:
pip install jupyter- Launch Jupyter Notebook:
jupyter notebook-
Open
Spam Mail Predictor.ipynb -
Run all cells sequentially to:
- Load and preprocess the data
- Train the Logistic Regression model
- Evaluate model performance
- Make predictions on new emails
The notebook includes a predictive system that can classify new emails:
input_mail = ["Your email text here"]
input_mail_features = feature_extraction.transform(input_mail)
prediction = model.predict(input_mail_features)
if prediction[0] == 1:
print('Ham mail')
else:
print('Spam mail')The model is evaluated on both training and test datasets:
- Training Data Accuracy: High accuracy on the data used for training
- Test Data Accuracy: Evaluation on unseen data to assess generalization
Accuracy metrics are printed during model evaluation in the notebook.
- Loads email data from CSV file
- Replaces null values with empty strings
- Analyzes data distribution and structure
- Converts categorical labels to numerical values
- Spam = 0, Ham = 1
- Uses TF-IDF Vectorizer to convert text into numerical features
- Parameters:
min_df=1: Minimum document frequencystop_words='english': Removes common English wordslowercase=True: Converts all text to lowercase
- 80% training data, 20% test data
- Random state set to 2 for reproducibility
- Trains Logistic Regression classifier on extracted features
- Binary classification model
- Calculates accuracy scores on training and test datasets
- Provides performance metrics for model assessment
- Pandas: Data manipulation and analysis
- NumPy: Numerical computing
- Scikit-learn: Machine learning algorithms and metrics
train_test_split: Data splittingLogisticRegression: Classification modelaccuracy_score: Performance evaluationTfidfVectorizer: Feature extraction
The Logistic Regression model achieves competitive accuracy in distinguishing between spam and legitimate emails, demonstrating the effectiveness of TF-IDF features combined with a linear classifier for text classification tasks.
- Experiment with other algorithms (Random Forest, SVM, Neural Networks)
- Implement cross-validation for more robust evaluation
- Add feature importance analysis
- Create a web interface for real-time predictions
- Optimize hyperparameters using GridSearchCV
- Implement additional preprocessing techniques (lemmatization, stemming)
[Your Name/Profile]
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request with improvements or bug fixes.
For questions or suggestions, please open an issue in the repository.