{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1b71a9c-e3b2-43d4-85fa-4329d90e47b9",
   "metadata": {},
   "source": [
    "# Practical-2 (Email Spam Detection)\n",
    "\n",
    "---\n",
    "\n",
    "Problem Statement: Classify the email using the binary classification method. Email Spam detection has two states: a) Normal State – Not Spam, b) Abnormal State – Spam. Use K-Nearest Neighbors and Support Vector Machine for classification. Analyze their performance.\n",
    "\n",
    "- Dataset link: https://www.kaggle.com/datasets/balaka18/email-spam-classification-dataset-csv\n",
    "- Dataset available in [Datasets](https://git.kska.io/sppu-be-comp-content/MachineLearning/src/branch/main/Datasets/) directory.\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "207c53ea-5e9c-49a2-90c5-2b28285c611a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---------------------------------------------------------------\n",
    "# EMAIL SPAM CLASSIFICATION USING KNN & SVM\n",
    "# ---------------------------------------------------------------\n",
    "\n",
    "# ---------------------------------------------------------------\n",
    "# 1. IMPORT LIBRARIES\n",
    "# ---------------------------------------------------------------\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "597c392f-12e8-4ed7-8ce2-0dd6289a7388",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['the', 'to', 'ect', 'and', 'for', 'of', 'a', 'you', 'hou', 'in',\n",
      "       ...\n",
      "       'connevey', 'jay', 'valued', 'lay', 'infrastructure', 'military',\n",
      "       'allowing', 'ff', 'dry', 'Prediction'],\n",
      "      dtype='object', length=3001)\n",
      "   the  to  ect  and  for  of    a  you  hou  in  ...  connevey  jay  valued  \\\n",
      "0    0   0    1    0    0   0    2    0    0   0  ...         0    0       0   \n",
      "1    8  13   24    6    6   2  102    1   27  18  ...         0    0       0   \n",
      "2    0   0    1    0    0   0    8    0    0   4  ...         0    0       0   \n",
      "3    0   5   22    0    5   1   51    2   10   1  ...         0    0       0   \n",
      "4    7   6   17    1    5   2   57    0    9   3  ...         0    0       0   \n",
      "\n",
      "   lay  infrastructure  military  allowing  ff  dry  Prediction  \n",
      "0    0               0         0         0   0    0           0  \n",
      "1    0               0         0         0   1    0           0  \n",
      "2    0               0         0         0   0    0           0  \n",
      "3    0               0         0         0   0    0           0  \n",
      "4    0               0         0         0   1    0           0  \n",
      "\n",
      "[5 rows x 3001 columns]\n"
     ]
    }
   ],
   "source": [
    "# ---------------------------------------------------------------\n",
    "# 2. LOAD DATASET\n",
    "# ---------------------------------------------------------------\n",
    "df = pd.read_csv(\"emails.csv\", encoding=\"ISO-8859-1\")  # Adjust path if needed\n",
    "\n",
    "# Drop unnecessary columns if present\n",
    "if \"Email No.\" in df.columns:\n",
    "    df = df.drop(columns=[\"Email No.\"])\n",
    "\n",
    "# Ensure label is integer\n",
    "df[\"Prediction\"] = df[\"Prediction\"].astype(int)\n",
    "\n",
    "# Features & target\n",
    "X = df.drop(columns=[\"Prediction\"])\n",
    "y = df[\"Prediction\"]\n",
    "\n",
    "# Print basic info\n",
    "print(df.columns)\n",
    "print(df.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "72b43964-1d8c-4f99-ad09-fd5e87c54ab5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---------------------------------------------------------------\n",
    "# 3. SPLIT DATA INTO TRAINING AND TESTING SETS\n",
    "# ---------------------------------------------------------------\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.2, random_state=42, stratify=y\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "9941a486-03b4-4770-abe9-d411197c8809",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "--- KNN Performance ---\n",
      "Accuracy: 0.8714975845410629\n",
      "Classification Report:\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "           0       0.93      0.88      0.91       735\n",
      "           1       0.74      0.85      0.79       300\n",
      "\n",
      "    accuracy                           0.87      1035\n",
      "   macro avg       0.84      0.86      0.85      1035\n",
      "weighted avg       0.88      0.87      0.87      1035\n",
      "\n",
      "Confusion Matrix:\n",
      " [[648  87]\n",
      " [ 46 254]]\n"
     ]
    }
   ],
   "source": [
    "# ---------------------------------------------------------------\n",
    "# 4. K-NEAREST NEIGHBORS CLASSIFIER\n",
    "# ---------------------------------------------------------------\n",
    "knn = KNeighborsClassifier(n_neighbors=5)\n",
    "knn.fit(X_train, y_train)\n",
    "y_pred_knn = knn.predict(X_test)\n",
    "\n",
    "print(\"\\n--- KNN Performance ---\")\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred_knn))\n",
    "print(\"Classification Report:\\n\", classification_report(y_test, y_pred_knn))\n",
    "print(\"Confusion Matrix:\\n\", confusion_matrix(y_test, y_pred_knn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "86ba6eeb-fa7a-4651-81bd-df6de11c387e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "--- SVM Performance ---\n",
      "Accuracy: 0.9671497584541063\n",
      "Classification Report:\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "           0       0.98      0.97      0.98       735\n",
      "           1       0.94      0.95      0.94       300\n",
      "\n",
      "    accuracy                           0.97      1035\n",
      "   macro avg       0.96      0.96      0.96      1035\n",
      "weighted avg       0.97      0.97      0.97      1035\n",
      "\n",
      "Confusion Matrix:\n",
      " [[716  19]\n",
      " [ 15 285]]\n"
     ]
    }
   ],
   "source": [
    "# ---------------------------------------------------------------\n",
    "# 5. SUPPORT VECTOR MACHINE CLASSIFIER\n",
    "# ---------------------------------------------------------------\n",
    "svm = SVC(kernel='linear', random_state=42)  # Linear kernel for binary classification\n",
    "svm.fit(X_train, y_train)\n",
    "y_pred_svm = svm.predict(X_test)\n",
    "\n",
    "print(\"\\n--- SVM Performance ---\")\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred_svm))\n",
    "print(\"Classification Report:\\n\", classification_report(y_test, y_pred_svm))\n",
    "print(\"Confusion Matrix:\\n\", confusion_matrix(y_test, y_pred_svm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a0ebfbf9-1ce6-4d22-9d28-5234df7df601",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37184 characters]",
      "text/plain": [
       "<Figure size 1200x500 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ---------------------------------------------------------------\n",
    "# 6. PLOTTING\n",
    "# ---------------------------------------------------------------\n",
    "fig, ax = plt.subplots(1, 2, figsize=(12, 5))\n",
    "\n",
    "sns.heatmap(confusion_matrix(y_test, y_pred_knn), annot=True, fmt=\"d\", cmap=\"Blues\", ax=ax[0])\n",
    "ax[0].set_title(\"KNN Confusion Matrix\")\n",
    "ax[0].set_xlabel(\"Predicted\")\n",
    "ax[0].set_ylabel(\"Actual\")\n",
    "\n",
    "sns.heatmap(confusion_matrix(y_test, y_pred_svm), annot=True, fmt=\"d\", cmap=\"Greens\", ax=ax[1])\n",
    "ax[1].set_title(\"SVM Confusion Matrix\")\n",
    "ax[1].set_xlabel(\"Predicted\")\n",
    "ax[1].set_ylabel(\"Actual\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "956f5a26-be04-44a1-b208-5a4b38280829",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
