This Python script implements a Spam Email Classifier using natural language processing (NLP) techniques and machine learning. The code uses scikit-learn for data preprocessing, feature extraction, and model evaluation. The classifier is trained on a labeled dataset of emails to distinguish between spam and non-spam (ham) messages.
- Clone the Repository:
git clone https://github.com/omm-prog/SpamEmailClassifier/ cd spam-email-classifier pip install pandas scikit-learn nltk python -m nltk.downloader stopwords python spam_classifier.py
- Reads a CSV dataset:
- Reads a CSV dataset containing 'text' and 'label' columns.
- Applies text preprocessing techniques, including:
- Removing special characters.
- Converting text to lowercase.
- Eliminating stop words.
- TF-IDF for Feature Extraction:
- Utilizes TF-IDF (Term Frequency-Inverse Document Frequency) for feature extraction.
- Converts text into numerical features.
- Multinomial Naive Bayes Classifier:
- Uses the Multinomial Naive Bayes classifier as an example.
- Splits the data into training and testing sets.
- Trains the model on the training data.
-
Predictions and Metrics:
- Predicts labels for the test set.
- Calculates metrics:
- Accuracy.
- Precision.
- Recall.
- F1 score.
-
Confusion Matrix:
- Generates a confusion matrix for detailed evaluation.
- Accuracy: 0.9507
- Precision: 0.9033
- Recall: 0.9249
- F1 Score: 0.9140
- Confusion Matrix: Where: [[713 29] [ 22 271]]