{"id":24302626,"url":"https://github.com/arif-miad/email-spam-classification-project","last_synced_at":"2026-05-09T07:34:25.531Z","repository":{"id":272464440,"uuid":"916673804","full_name":"Arif-miad/Email-Spam-Classification-Project","owner":"Arif-miad","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-14T15:24:53.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-14T16:55:20.143Z","etag":null,"topics":["keras","machine-learning","nlp","nltk","numpy","pandas-dataframe","svm-classifier"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Arif-miad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-14T14:59:40.000Z","updated_at":"2025-01-14T15:26:48.000Z","dependencies_parsed_at":"2025-01-14T16:55:54.307Z","dependency_job_id":"86d5427a-3805-485d-8adb-27e83e429f3f","html_url":"https://github.com/Arif-miad/Email-Spam-Classification-Project","commit_stats":null,"previous_names":["arif-miad/email-spam-classification-project"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arif-miad%2FEmail-Spam-Classification-Project","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arif-miad%2FEmail-Spam-Classification-Project/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arif-miad%2FEmail-Spam-Classification-Project/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arif-miad%2FEmail-Spam-Classification-Project/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Arif-miad","download_url":"https://codeload.github.com/Arif-miad/Email-Spam-Classification-Project/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242225788,"owners_count":20092656,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["keras","machine-learning","nlp","nltk","numpy","pandas-dataframe","svm-classifier"],"created_at":"2025-01-17T00:17:45.376Z","updated_at":"2026-05-09T07:34:25.504Z","avatar_url":"https://github.com/Arif-miad.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Email-Spam-Classification-Project\n\n\n\n### Dataset Overview\n\n1. **Dataset Name**\n   - Email Spam Classification Dataset\n\n2. **Description**\n   - This dataset contains email messages labeled as spam or ham (non-spam).\n   - Each entry includes the full content of the email and its classification (0 for ham, 1 for spam).\n   - Total observations: 5,728\n\n3. **Columns**\n   - `text`: Full content of the email message.\n   - `spam`: Binary label indicating whether the email is spam (1) or ham (0).\n\n4. **Dataset Characteristics**\n   - **Format**: Tab-delimited (easy to import and process).\n   - **Content**: Diverse range of topics and styles typical of real-world emails.\n   - **Use Case**: Suitable for natural language processing (NLP), machine learning, and email filtering system development.\n\n5. **Objective**\n   - Perform Exploratory Data Analysis (EDA) to understand data distribution, text characteristics, and common patterns in spam and ham emails.\n   - Build and evaluate machine learning models (e.g., Logistic Regression, Random Forest) for accurate email classification.\n\n6. **Tasks and Steps**\n   - **Data Loading and Inspection**\n     - Load dataset into Pandas DataFrame.\n     - Check basic statistics and structure.\n     - Handle missing values if any.\n\n   - **Exploratory Data Analysis (EDA)**\n     - Visualize class distribution (spam vs. ham).\n     - Analyze text length distribution for spam and ham emails.\n     - Generate word clouds to identify common words in spam and ham emails.\n     - Explore common words and n-grams using techniques like TF-IDF.\n\n   - **Data Preprocessing**\n     - Clean and preprocess text data (lowercase, remove punctuation, etc.).\n     - Convert text data into numerical features suitable for machine learning models.\n     - Handle categorical variables (if any).\n\n   - **Model Building and Evaluation**\n     - Split dataset into training and testing sets.\n     - Implement various classification models (Logistic Regression, SVM, etc.).\n     - Evaluate model performance using metrics like accuracy, precision, recall, F1 score, and ROC-AUC.\n     - Compare and select the best-performing model based on evaluation metrics and business requirements.\n\n7. **Conclusion**\n   - Summarize findings from EDA and model evaluation.\n   - Discuss insights gained and potential improvements for future work.\n \n\n\n\n## Overview\nThis project focuses on classifying emails as either spam (1) or ham (0). Using machine learning and natural language processing (NLP) techniques, we perform exploratory data analysis (EDA) and develop robust models to accurately classify emails.\n\n---\n\n## Table of Contents\n1. [Introduction](#introduction)  \n2. [Dataset Description](#dataset-description)  \n3. [Key Objectives](#key-objectives)  \n4. [Workflow](#workflow)  \n5. [Requirements](#requirements)  \n6. [Data Loading and Preprocessing](#data-loading-and-preprocessing)  \n7. [Exploratory Data Analysis (EDA)](#exploratory-data-analysis-eda)  \n8. [Feature Engineering](#feature-engineering)  \n9. [Model Development](#model-development)  \n10. [Performance Metrics and Equations](#performance-metrics-and-equations)  \n11. [Results](#results)  \n12. [Conclusion](#conclusion)  \n13. [References](#references)  \n\n---\n\n## Introduction\nSpam emails have become a major concern in the digital era. This project aims to create a robust email classification system using the Email Spam Classification Dataset, which consists of text-based emails and their labels (spam or ham).  \n\n---\n\n## Dataset Description\n- **Dataset Name:** Email Spam Classification  \n- **Columns:**\n  - `text`: Content of the email.\n  - `spam`: Label indicating spam (1) or ham (0).  \n- **Total Observations:** 5,728 emails  \n- **Format:** Tab-delimited  \n\n---\n\n## Key Objectives\n1. Perform detailed exploratory data analysis (EDA).  \n2. Clean and preprocess the text data.  \n3. Engineer features using tokenization, TF-IDF, etc.  \n4. Build and evaluate classification models.  \n5. Compare models using performance metrics.  \n\n---\n\n## Workflow\n### Step-by-Step Implementation:\n1. Import necessary libraries.  \n2. Load the dataset.  \n3. Check for missing values.  \n4. Visualize spam vs. ham distribution.  \n5. Analyze word frequencies for spam and ham.  \n6. Generate word clouds for better insights.  \n7. Preprocess text data (lowercase, punctuation removal, etc.).  \n8. Remove stopwords and perform lemmatization.  \n9. Perform text vectorization using TF-IDF.  \n10. Split the dataset into training and testing sets.  \n11. Build the following models:\n    - Logistic Regression  \n    - Naive Bayes  \n    - Support Vector Machine (SVM)  \n    - Random Forest  \n    - Gradient Boosting  \n12. Evaluate models using performance metrics:\n    - Accuracy  \n    - Precision  \n    - Recall  \n    - F1 Score  \n    - ROC and AUC  \n13. Perform hyperparameter tuning using GridSearchCV.  \n14. Visualize the confusion matrix.  \n15. Compare model performance and finalize the best model.  \n\n---\n\n## Requirements\nInstall the required Python libraries using the following command:\n```bash\npip install numpy pandas matplotlib seaborn scikit-learn wordcloud nltk\n```\n\n---\n\n## Data Loading and Preprocessing\n### Loading Dataset:\n```python\n# Step 1: Import required libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, roc_curve, accuracy_score\n\n# Step 2: Load the dataset\ndata = pd.read_csv(\"email_spam_dataset.csv\")  # Replace with your dataset path\ndata.columns = ['text', 'spam']  # Ensure column names are correct\n\n# Step 3: Inspect the dataset\nprint(data.head())\nprint(data.info())\nprint(data.describe())\n\n# Step 4: Check for missing values\nprint(\"Missing values:\\n\", data.isnull().sum())\n\n# Step 5: Check class distribution\nsns.countplot(data['spam'])\nplt.title(\"Spam vs Ham Distribution\")\nplt.show()\n\n# Step 6: Convert target column to numeric if not already\ndata['spam'] = data['spam'].map({'ham': 0, 'spam': 1})  # Adjust based on dataset\n\n# Step 7: Analyze text length\ndata['text_length'] = data['text'].apply(len)\nsns.histplot(data, x='text_length', hue='spam', kde=True)\nplt.title(\"Text Length Distribution\")\nplt.show()\n\n# Step 8: Word cloud for spam\nfrom wordcloud import WordCloud\nspam_words = ' '.join(data[data['spam'] == 1]['text'])\nwordcloud = WordCloud(width=800, height=400, background_color='black').generate(spam_words)\nplt.imshow(wordcloud, interpolation='bilinear')\nplt.axis('off')\nplt.title(\"Word Cloud for Spam Emails\")\nplt.show()\n\n# Step 9: Word cloud for ham\nham_words = ' '.join(data[data['spam'] == 0]['text'])\nwordcloud = WordCloud(width=800, height=400, background_color='white').generate(ham_words)\nplt.imshow(wordcloud, interpolation='bilinear')\nplt.axis('off')\nplt.title(\"Word Cloud for Ham Emails\")\nplt.show()\n\n# Step 10: Analyze most common words\nfrom sklearn.feature_extraction.text import CountVectorizer\ncv = CountVectorizer(stop_words='english', max_features=20)\ncommon_words = cv.fit_transform(data['text'])\ncommon_words_df = pd.DataFrame(cv.get_feature_names_out(), columns=['Word'])\nprint(\"Most common words:\\n\", common_words_df)\n\n# Step 11: Preprocess text (lowercase, remove punctuation, etc.)\nimport string\nimport re\n\ndef preprocess_text(text):\n    text = text.lower()  # Convert to lowercase\n    text = re.sub(r'\\d+', '', text)  # Remove numbers\n    text = text.translate(str.maketrans('', '', string.punctuation))  # Remove punctuation\n    text = text.strip()  # Remove leading/trailing spaces\n    return text\n\ndata['cleaned_text'] = data['text'].apply(preprocess_text)\n\n# Step 12: Split the dataset\nX = data['cleaned_text']\ny = data['spam']\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Step 13: Transform text data using TF-IDF\ntfidf = TfidfVectorizer(max_features=5000)\nX_train_tfidf = tfidf.fit_transform(X_train)\nX_test_tfidf = tfidf.transform(X_test)\n\n# Step 14: Build logistic regression model\nlr_model = LogisticRegression()\nlr_model.fit(X_train_tfidf, y_train)\n\n# Step 15: Evaluate logistic regression model\ny_pred_lr = lr_model.predict(X_test_tfidf)\nprint(\"Logistic Regression Classification Report:\\n\", classification_report(y_test, y_pred_lr))\nprint(\"Confusion Matrix:\\n\", confusion_matrix(y_test, y_pred_lr))\n\n# Step 16: Random Forest model\nrf_model = RandomForestClassifier()\nrf_model.fit(X_train_tfidf, y_train)\n\n# Step 17: Evaluate Random Forest model\ny_pred_rf = rf_model.predict(X_test_tfidf)\nprint(\"Random Forest Classification Report:\\n\", classification_report(y_test, y_pred_rf))\n\n# Step 18: Support Vector Machine (SVM) model\nsvm_model = SVC(probability=True)\nsvm_model.fit(X_train_tfidf, y_train)\n\n# Step 19: Evaluate SVM model\ny_pred_svm = svm_model.predict(X_test_tfidf)\nprint(\"SVM Classification Report:\\n\", classification_report(y_test, y_pred_svm))\n\n# Step 20: ROC Curve for Logistic Regression\nlr_probs = lr_model.predict_proba(X_test_tfidf)[:, 1]\nfpr, tpr, _ = roc_curve(y_test, lr_probs)\nplt.plot(fpr, tpr, label=\"Logistic Regression\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC Curve\")\nplt.legend()\nplt.show()\n\n# Step 21: Compare model performance\nmodels = ['Logistic Regression', 'Random Forest', 'SVM']\naccuracies = [accuracy_score(y_test, y_pred_lr), accuracy_score(y_test, y_pred_rf), accuracy_score(y_test, y_pred_svm)]\nplt.bar(models, accuracies, color=['blue', 'green', 'red'])\nplt.ylabel(\"Accuracy\")\nplt.title(\"Model Comparison\")\nplt.show()\n\n# Step 22: Hyperparameter tuning for Random Forest\nfrom sklearn.model_selection import GridSearchCV\nparam_grid = {'n_estimators': [50, 100, 200], 'max_depth': [10, 20, None]}\ngrid_rf = GridSearchCV(RandomForestClassifier(), param_grid, cv=3, scoring='accuracy')\ngrid_rf.fit(X_train_tfidf, y_train)\nprint(\"Best Random Forest Parameters:\\n\", grid_rf.best_params_)\n\n# Step 23: Train tuned Random Forest\nrf_tuned = grid_rf.best_estimator_\nrf_tuned.fit(X_train_tfidf, y_train)\n\n# Step 24: Evaluate tuned Random Forest\ny_pred_rf_tuned = rf_tuned.predict(X_test_tfidf)\nprint(\"Tuned Random Forest Accuracy:\", accuracy_score(y_test, y_pred_rf_tuned))\n\n# Step 25: Save the model and vectorizer\nimport joblib\njoblib.dump(rf_tuned, \"spam_classifier_model.pkl\")\njoblib.dump(tfidf, \"tfidf_vectorizer.pkl\")\n\n# Step 26: Load saved model\nloaded_model = joblib.load(\"spam_classifier_model.pkl\")\nloaded_vectorizer = joblib.load(\"tfidf_vectorizer.pkl\")\n\n# Step 27: Test saved model\nnew_email = [\"Congratulations! You've won a prize!\"]\nnew_email_tfidf = loaded_vectorizer.transform(new_email)\nprint(\"Prediction for new email:\", loaded_model.predict(new_email_tfidf))\n\n# Step 28: Feature importance for Random Forest\nfeature_importances = rf_tuned.feature_importances_\ntop_features = np.argsort(feature_importances)[-10:]\nplt.barh(np.array(tfidf.get_feature_names_out())[top_features], feature_importances[top_features])\nplt.title(\"Top Features in Random Forest\")\nplt.show()\n\n# Step 29: Check misclassified emails\nmisclassified = X_test[(y_test != y_pred_rf_tuned)]\nprint(\"Misclassified Emails:\\n\", misclassified)\n\n# Step 30: Summarize findings\nprint(\"Summary: Logistic Regression performed better in terms of [metrics]. Random Forest had higher interpretability due to feature importance.\")\n\n```\n![](https://github.com/Arif-miad/Email-Spam-Classification-Project/blob/main/e1.PNG)\n---\n\n## Performance Metrics and Equations\n### Key Metrics:\n1. **Accuracy**:\n![output](https://github.com/Arif-miad/Email-Spam-Classification-Project/blob/main/eial.PNG)\n\n---\n\n## Results\n- **Best Model:** Logistic Regression achieved the highest accuracy (e.g., 95%).\n- **Key Insights:**\n  - Spam emails often have shorter text lengths.\n  - Common spam words include \"offer,\" \"free,\" and \"win.\"  \n\n---\n\n## Conclusion\nThis project demonstrates a complete workflow for spam classification, starting from data loading to model evaluation. The results show that NLP and machine learning techniques can effectively classify emails as spam or ham.  \n\n---\n\n## References\n1. [Scikit-learn Documentation](https://scikit-learn.org/)\n2. [Kaggle - Spam Email Datasets](https://www.kaggle.com/)\n3. [NLTK Library](https://www.nltk.org/)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farif-miad%2Femail-spam-classification-project","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farif-miad%2Femail-spam-classification-project","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farif-miad%2Femail-spam-classification-project/lists"}