Files
labs/Module-4/2.Spam_Detection_SVMs.ipynb
T

213 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!pip install mlxtend"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tXKmqh2JP1Io"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load data & split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tXKmqh2JP1Io"
},
"outputs": [],
"source": [
"df = pd.read_csv('datasets-1/sms_spam_svm.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1K8dyw4eP_08"
},
"outputs": [],
"source": [
"y = df.iloc[:, 0].values\n",
"y = np.where(y == 'spam', -1, 1)\n",
"\n",
"x = df.iloc[:, [1, 2]].values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "dCPECPPgQLPq"
},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
" x, y, test_size=0.3, random_state=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Training using SVC"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SIxi3X0BQpyG"
},
"outputs": [],
"source": [
"from sklearn.svm import SVC\n",
"\n",
"# linear, poly, rbf, sigmoid\n",
"svm = SVC(kernel='rbf', gamma='auto', random_state=1)\n",
"svm.fit(X_train, y_train)\n",
"\n",
"y_pred = svm.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 384
},
"id": "_PZ3efgQR8K8",
"outputId": "45df8ea2-5e8e-4fe1-bb10-6932aa900ca1"
},
"outputs": [],
"source": [
"from mlxtend.plotting import plot_decision_regions\n",
"\n",
"\n",
"import matplotlib.pyplot as plt\n",
"X_combined = np.vstack((X_train, X_test))\n",
"y_combined = np.hstack((y_train, y_test))\n",
"\n",
"\n",
"\n",
"plot_decision_regions(X_combined, y_combined, clf=svm)\n",
"\n",
"plt.xlabel('suspect words')\n",
"plt.ylabel('spam or ham')\n",
"plt.legend(loc='upper right')\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-Q5Ue3AETWrn",
"outputId": "b0aa724e-828f-4b93-b6b5-e5e60a166b15"
},
"outputs": [],
"source": [
"from sklearn.metrics import accuracy_score\n",
"print('Misclassified samples: %d' % (y_test != y_pred).sum())\n",
"print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import classification_report , confusion_matrix\n",
"\n",
"print(\"Confusion Matrix:\")\n",
"print(confusion_matrix(y_test, y_pred))\n",
"\n",
"print(\"\\nClassification Report:\")\n",
"print(classification_report(y_test, y_pred))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Spam detection with SVMs.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.8"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}