{ "cells": [ { "cell_type": "markdown", "id": "c49480db", "metadata": {}, "source": [ "(chapter11_part1)=\n", "\n", "# Maximum Margin Models\n", "\n", "- This is a supplement material for the [Machine Learning Simplified](https://themlsbook.com) book. It sheds light on Python implementations of the topics discussed while all detailed explanations can be found in the book. \n", "- I also assume you know Python syntax and how it works. If you don't, I highly recommend you to take a break and get introduced to the language before going forward with my code. \n", "- This material can be downloaded as a Jupyter notebook (Download button in the upper-right corner -> `.ipynb`) to reproduce the code and play around with it. \n", "\n", "\n", "This notebook is a supplement for *Chapter 11. Maximum Margin Models* of **Machine Learning For Everyone** book.\n", "\n", "## 1. Required Libraries\n", "\n", "Let's import required libraries:" ] }, { "cell_type": "code", "execution_count": 1, "id": "a24d90df", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from sklearn.datasets import make_classification\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.svm import SVC\n", "from sklearn.metrics import accuracy_score, confusion_matrix\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "64dcfaaf", "metadata": {}, "source": [ "## 2. Create a Synthetic Dataset\n", "\n", "To demonstrate the application of a Maximum Margin Model using both Linear SVM (Support Vector Machine) and Kernelized SVM, let's first create a synthetic dataset in Python. We will use this dataset to train and evaluate our models.\n", "\n", "We'll use make_classification from scikit-learn to generate a binary classification dataset." ] }, { "cell_type": "code", "execution_count": 2, "id": "2f30c9b4", "metadata": {}, "outputs": [], "source": [ "# Step 1: Create a synthetic dataset\n", "X, y = make_classification(n_samples=200, n_features=2, n_redundant=0, n_informative=2,\n", " random_state=1, n_clusters_per_class=1, flip_y=0.1)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "f7c4247c", "metadata": {}, "outputs": [], "source": [ "# Splitting the dataset into training and testing sets\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)" ] }, { "cell_type": "markdown", "id": "400aabc1", "metadata": {}, "source": [ "## 2. Implement Linear SVM and Kernelized SVM\n", "\n", "We will use the SVC (Support Vector Classifier) from scikit-learn, applying both linear and kernelized (e.g., RBF) approaches." ] }, { "cell_type": "code", "execution_count": 4, "id": "f6d597ff", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
SVC(kernel='linear')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "SVC(kernel='linear')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Step 2: Implement SVM Models\n", "# Linear SVM\n", "linear_svm = SVC(kernel='linear', C=1.0)\n", "linear_svm.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 5, "id": "65885902", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
SVC()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "SVC()" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Kernelized SVM (RBF kernel)\n", "rbf_svm = SVC(kernel='rbf', C=1.0, gamma='scale')\n", "rbf_svm.fit(X_train, y_train)" ] }, { "cell_type": "markdown", "id": "e852d33f", "metadata": {}, "source": [ "## 3. Make Prediction" ] }, { "cell_type": "code", "execution_count": 6, "id": "f134c7b3", "metadata": {}, "outputs": [], "source": [ "# Step 3: Evaluate the models\n", "# Predictions from both models\n", "y_pred_linear = linear_svm.predict(X_test)\n", "y_pred_rbf = rbf_svm.predict(X_test)" ] }, { "cell_type": "markdown", "id": "28e30b5c", "metadata": {}, "source": [ "## 4. Evaluate the Models" ] }, { "cell_type": "code", "execution_count": 7, "id": "83ce989d", "metadata": {}, "outputs": [], "source": [ "# Accuracy and Confusion Matrix\n", "accuracy_linear = accuracy_score(y_test, y_pred_linear)\n", "accuracy_rbf = accuracy_score(y_test, y_pred_rbf)\n", "cm_linear = confusion_matrix(y_test, y_pred_linear)\n", "cm_rbf = confusion_matrix(y_test, y_pred_rbf)" ] }, { "cell_type": "code", "execution_count": 8, "id": "d93b0827", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[19, 5],\n", " [ 5, 21]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm_linear" ] }, { "cell_type": "code", "execution_count": 9, "id": "6753ca59", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[18, 6],\n", " [ 3, 23]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm_rbf" ] }, { "cell_type": "code", "execution_count": 10, "id": "800fbab0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Linear SVM Accuracy: 0.8\n", "Linear SVM Confusion Matrix:\n", " [[19 5]\n", " [ 5 21]]\n", "Kernelized SVM Accuracy: 0.82\n", "Kernelized SVM Confusion Matrix:\n", " [[18 6]\n", " [ 3 23]]\n" ] } ], "source": [ "# Print the results\n", "print(\"Linear SVM Accuracy:\", accuracy_linear)\n", "print(\"Linear SVM Confusion Matrix:\\n\", cm_linear)\n", "print(\"Kernelized SVM Accuracy:\", accuracy_rbf)\n", "print(\"Kernelized SVM Confusion Matrix:\\n\", cm_rbf)" ] }, { "cell_type": "markdown", "id": "4270cdbd", "metadata": {}, "source": [ "## 5. Plotting the dataset and the decision boundary\n", "\n", "\n", "```pyt{code-cell} ipython3hon\n", "# Plotting the dataset and the decision boundary\n", "def plot_svc_decision_function(model, ax=None, plot_support=True):\n", " \"\"\"Plot the decision function for a 2D SVC\"\"\"\n", " if ax is None:\n", " ax = plt.gca()\n", " xlim = ax.get_xlim()\n", " ylim = ax.get_ylim()\n", " \n", " # create grid to evaluate model\n", " x = np.linspace(xlim[0], xlim[1], 30)\n", " y = np.linspace(ylim[0], ylim[1], 30)\n", " Y, X = np.meshgrid(y, x)\n", " xy = np.vstack([X.ravel(), Y.ravel()]).T\n", " P = model.decision_function(xy).reshape(X.shape)\n", " \n", " # plot decision boundary and margins\n", " ax.contour(X, Y, P, colors='k',\n", " levels=[-1, 0, 1], alpha=0.5,\n", " linestyles=['--', '-', '--'])\n", " \n", " # plot support vectors\n", " if plot_support:\n", " ax.scatter(model.support_vectors_[:, 0],\n", " model.support_vectors_[:, 1],\n", " s=50, linewidth=1, facecolors='none', edgecolors='k')\n", " ax.set_xlim(xlim)\n", " ax.set_ylim(ylim)\n", "\n", "# Plotting\n", "plt.figure(figsize=(12, 6))\n", "plt.subplot(1, 2, 1)\n", "plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap='autumn')\n", "plot_svc_decision_function(linear_svm)\n", "plt.title(\"Linear SVM\")\n", "\n", "plt.subplot(1, 2, 2)\n", "plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap='autumn')\n", "plot_svc_decision_function(rbf_svm)\n", "plt.title(\"Kernelized SVM (RBF)\")\n", "\n", "plt.show()\n", "```" ] } ], "metadata": { "jupytext": { "formats": "md:myst", "text_representation": { "extension": ".md", "format_name": "myst" } }, "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.10.7" }, "source_map": [ 11, 29, 37, 46, 54, 57, 64, 72, 76, 81, 86, 91, 100, 105, 110, 116 ] }, "nbformat": 4, "nbformat_minor": 5 }