{ "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.
SVC(kernel='linear')
SVC()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
SVC()