tahamajs's picture
download
raw
28.3 kB
{
"cells": [
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from torchvision.datasets import MNIST\n",
"from torchvision.transforms import transforms\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.svm import SVC\n",
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import accuracy_score\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Software Framework and Algorithmic Tools**—This experiment combines deep‐learning data pipelines with classical machine‐learning classifiers by importing both **PyTorch** and **Scikit‐Learn** components. **NumPy** is used as the foundational library for multi‐dimensional array operations and numerical routines. From **torchvision**, the **MNIST** dataset class provides seamless download and iteration over handwritten‐digit images, while **transforms** enables preprocessing steps such as tensor conversion and normalization. **Matplotlib’s** `pyplot` module facilitates creation of publication‐quality figures for data exploration and result visualization. \n",
"\n",
"On the classical‐ML side, **Scikit‐Learn** supplies two core classifiers: **Support Vector Classifier** (`SVC`) for kernel‐based maximum‐margin separation, and **LogisticRegression** for regularized linear probability modeling. **GridSearchCV** orchestrates exhaustive hyperparameter tuning via cross‐validation, systematically evaluating parameter grids for both SVM and logistic models. Finally, **accuracy_score** computes the proportion of correctly classified examples, serving as the primary evaluation metric across all methods."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load MNIST"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"MNIST_MEAN = 0.1307\n",
"MNIST_STD = 0.3081\n",
"\n",
"transform = transforms.Compose([\n",
" transforms.ToTensor(), \n",
" transforms.Normalize((MNIST_MEAN,), (MNIST_STD,))\n",
" ])\n",
"\n",
"train_dataset = MNIST(root='../data', train=True, download=True, transform=transform)\n",
"test_dataset = MNIST(root='../data', train=False, download=True, transform=transform)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Data Loading and Normalization—** The MNIST handwritten‑digit dataset is loaded via the `MNIST` class from `torchvision.datasets`, with separate instances created for the training and test splits. To ensure stable convergence during network training, each pixel intensity is first converted to a floating‑point tensor in the range \\([0,1]\\), then standardized using the dataset’s global mean (\\(0.1307\\)) and standard deviation (\\(0.3081\\)). This two‑step transform pipeline—tensor conversion followed by channel‑wise normalization—is applied uniformly to both splits, guaranteeing that all examples share the same input distribution when fed into the model."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Show Image Function"
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [],
"source": [
"def show_image(image, title):\n",
" plt.figure(figsize=(4, 4))\n",
" plt.imshow(image, cmap='gray_r')\n",
" plt.title(title)\n",
" plt.axis('off')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Image Display Utility**—A dedicated helper routine was implemented to render single‐channel digit images in a consistent, publication‐quality format. For any input image array, this utility instantiates a fixed‐size plotting canvas (4″ × 4″), displays the pixel data using a reversed‐grayscale colormap to emphasize stroke intensity against the background, suppresses axis tick marks and labels for visual clarity, and centers a descriptive title above the figure. By encapsulating these common plotting steps, the helper ensures that all digit visualizations—original, shifted, or otherwise transformed—adhere to a uniform aesthetic suitable for side‑by‑side comparison in the final report."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Train and Test data"
]
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [],
"source": [
"X_train = np.array(train_dataset.data).reshape(-1, 28 * 28)[:1000]\n",
"y_train = np.array(train_dataset.targets)[:1000]\n",
"X_test = np.array(test_dataset.data).reshape(-1, 28 * 28)[:100]\n",
"y_test = np.array(test_dataset.targets)[:100]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Data Preparation for Classical Classifiers—** To interface the PyTorch‐loaded MNIST data with Scikit‑Learn, the tensor attributes `train_dataset.data` and `test_dataset.data` are first converted to NumPy arrays. Each 28×28 image is then flattened into a 784‑dimensional feature vector via a reshape operation. For computational tractability during grid‐search tuning and proof‑of‑concept evaluation, we subsample the dataset by taking the first 1 000 training examples (`X_train`, `y_train`) and the first 100 test examples (`X_test`, `y_test`). This controlled downsampling accelerates fitting of SVM and logistic regression models while preserving a representative mix of digit classes for performance assessment."
]
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Digit Count(train data)\n",
"----- -----------------\n",
"0 97\n",
"1 116\n",
"2 99\n",
"3 93\n",
"4 105\n",
"5 92\n",
"6 94\n",
"7 117\n",
"8 87\n",
"9 100\n"
]
}
],
"source": [
"digits, counts = np.unique(y_train, return_counts=True)\n",
"print(\"Digit Count(train data)\")\n",
"print(\"----- -----------------\")\n",
"for digit, count in zip(digits, counts):\n",
" print(f\"{digit:<8}{count}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Training‐Subset Class Balance**—Before fitting the classical classifiers, we verified the label distribution in our 1 000‑sample training subset by counting the occurrences of each digit (0–9). As shown in Table A, each class appears between 87 and 117 times, indicating a roughly uniform representation across all ten digits. This near–balanced sampling mitigates bias toward any particular class during hyperparameter tuning and model evaluation.\n",
"\n",
"| Digit | Count (train data) |\n",
"|-------|--------------------:|\n",
"| 0 | 97 |\n",
"| 1 | 116 |\n",
"| 2 | 99 |\n",
"| 3 | 93 |\n",
"| 4 | 105 |\n",
"| 5 | 92 |\n",
"| 6 | 94 |\n",
"| 7 | 117 |\n",
"| 8 | 87 |\n",
"| 9 | 100 |\n",
"\n",
"*Table A. Frequency of each digit in the 1 000‑sample training subset.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Kernel: Linear"
]
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Best Score: 0.88\n",
"Best Parameters: {'C': 1e-06}\n"
]
}
],
"source": [
"param_grid = {\n",
" 'C': [1e-7, 1e-6, 1e-5, 1e-4, 0.01, 0.1, 1, 10]\n",
"}\n",
"\n",
"svm = SVC(kernel='linear')\n",
"\n",
"grid_search_linear = GridSearchCV(svm, param_grid, cv=5)\n",
"\n",
"grid_search_linear.fit(X_train, y_train)\n",
"\n",
"print(f\"Best Score: {grid_search_linear.best_score_:.2f}\")\n",
"print(f\"Best Parameters: {grid_search_linear.best_params_}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Linear SVM Hyperparameter Tuning**—To optimize the capacity of a linear‐kernel SVM on our 1 000‑sample MNIST training subset, we conducted a five‑fold cross‐validation over the regularization parameter $C$, scanning a logarithmic grid from $10^{-7}$ up to $10^{1}$. For each candidate value of $C$, the SVM was trained on four folds and evaluated on the held‑out fold, with the mean validation accuracy recorded. The grid search yielded a best cross‑validation score of **0.88** at $C=10^{-6}$, indicating that a very small regularization strength (i.e., a wide margin and high slack penalty) provided the best balance between underfitting and overfitting on this limited training set.\n",
"\n",
"**Model Selection Insight**—The fact that $C=10^{-6}$ outperformed larger values suggests that strong regularization was necessary to prevent the linear SVM from fitting noise in the high‑dimensional pixel space. In practice, this small‑$C$ regime yields a simpler decision boundary with fewer support vectors, which can enhance generalization—especially when the number of training samples is small relative to the feature dimension. The selected model will thus be evaluated on the held‑out test set to assess its final classification accuracy."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Kernel: RBF"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Best Score: 0.91\n",
"Best Parameters: {'C': 10, 'gamma': 'scale'}\n"
]
}
],
"source": [
"param_grid = {\n",
" 'C': [0.01, 0.1, 1, 10, 100],\n",
" 'gamma': ['scale', 'auto']\n",
"}\n",
"\n",
"svm = SVC(kernel='rbf')\n",
"\n",
"grid_search_rbf = GridSearchCV(svm, param_grid, cv=5)\n",
"\n",
"grid_search_rbf.fit(X_train, y_train)\n",
"\n",
"print(f\"Best Score: {grid_search_rbf.best_score_:.2f}\")\n",
"print(f\"Best Parameters: {grid_search_rbf.best_params_}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Nonlinear SVM Hyperparameter Optimization—** To capture complex, non‑linear digit patterns in the high‑dimensional pixel space, we replaced the linear kernel with a radial basis function (RBF) kernel and again conducted a five‑fold cross‐validation over both the regularization parameter $C$ and the kernel width parameter $\\gamma$. We evaluated $C$ in $\\{0.01,\\,0.1,\\,1,\\,10,\\,100\\}$ and tested both Scikit‑Learn’s `scale` and `auto` strategies for $\\gamma$. \n",
"\n",
"**Cross‑Validation Results—** The grid search identified a best mean validation accuracy of **0.91** with $C=10$ and $\\gamma=\\text{scale}$. This combination balances model flexibility against overfitting: a moderately large $C$ permits tighter decision boundaries around support vectors, while `scale` (the default heuristic $\\gamma=1/(\\text{n\\_features}\\times\\mathrm{Var}(X))$) adaptively adjusts the kernel width to the overall data variance. \n",
"\n",
"**Model Selection Insight—** Achieving a 3 % improvement over the best linear SVM score demonstrates that the RBF kernel effectively leverages non‑linear relationships between pixel intensities to distinguish digit classes, even when trained on just 1 000 examples. The selected RBF SVM ($C=10,\\;\\gamma=\\text{scale}$) is expected to yield strong generalization performance when evaluated on the held‑out test set."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Kernel: Polynomial Degree 2"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Best Score: 0.89\n",
"Best Parameters: {'C': 10, 'gamma': 'scale'}\n"
]
}
],
"source": [
"param_grid = {\n",
" 'C': [0.1, 1, 10, 100],\n",
" 'gamma': ['scale', 'auto']\n",
"}\n",
"\n",
"svm = SVC(kernel='poly', degree=2)\n",
"\n",
"grid_search_poly = GridSearchCV(svm, param_grid, cv=5)\n",
"\n",
"grid_search_poly.fit(X_train, y_train)\n",
"\n",
"\n",
"print(f\"Best Score: {grid_search_poly.best_score_:.2f}\")\n",
"print(f\"Best Parameters: {grid_search_poly.best_params_}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Polynomial‑Kernel SVM Hyperparameter Tuning**—To explore moderate nonlinearity, we trained an SVM with a second‑degree polynomial kernel ($\\deg=2$) on the same 1 000‑sample MNIST subset. We performed a five‑fold cross‑validation over the regularization constant $C\\in\\{0.1,\\,1,\\,10,\\,100\\}$ and the kernel coefficient $\\gamma\\in\\{\\text{scale},\\,\\text{auto}\\}$. The grid search identified a best mean validation accuracy of **0.89** at $C=10$ and $\\gamma=\\text{scale}$. This performance slightly improves over the linear SVM baseline (0.88) but remains below the RBF kernel’s result (0.91), indicating that quadratic decision boundaries capture more complex pixel‑intensity relationships than hyperplanes alone, yet lack the full flexibility of Gaussian kernels. Selecting $C=10$ ensures sufficient capacity to fit the curved class boundary without overfitting, while the scale heuristic for $\\gamma$ adapts the kernel width to the feature‑space variance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Best SVM"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Best Parameters: {'C': 10, 'gamma': 'scale'}\n",
"SVM Train Accuracy: 1.00\n",
"SVM Test Accuracy: 0.94\n"
]
}
],
"source": [
"best_svm = grid_search_rbf.best_estimator_\n",
"\n",
"y_train_pred = best_svm.predict(X_train)\n",
"y_test_pred = best_svm.predict(X_test)\n",
"\n",
"train_accuracy = accuracy_score(y_train, y_train_pred)\n",
"test_accuracy = accuracy_score(y_test, y_test_pred)\n",
"\n",
"print(f\"Best Parameters: {grid_search_rbf.best_params_}\")\n",
"print(f\"SVM Train Accuracy: {train_accuracy:.2f}\")\n",
"print(f\"SVM Test Accuracy: {test_accuracy:.2f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**RBF SVM Final Evaluation**—Using the RBF‐kernel SVM selected by cross‐validation (\\(C=10,\\;\\gamma=\\text{scale}\\)), we observe a **100 %** accuracy on the 1 000‑sample training subset and **94 %** accuracy on the 100‑sample test set. This result confirms that the chosen model fully fits the limited training data while retaining strong generalization to unseen examples, outperforming both its linear (88 % CV) and quadratic‐kernel (89 % CV) counterparts. The slight performance gap between training and test sets indicates only mild overfitting, underscoring the effectiveness of the RBF kernel and regularization configuration even under severe downsampling of the MNIST dataset."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SVM VS. Logistic Regression"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Logistic Regression Test Accuracy: 0.90\n"
]
}
],
"source": [
"logistic = LogisticRegression(max_iter=300)\n",
"logistic.fit(X_train, y_train)\n",
"logistic_y_pred = logistic.predict(X_test)\n",
"lr_accuracy = accuracy_score(y_test, logistic_y_pred)\n",
"print(f\"Logistic Regression Test Accuracy: {lr_accuracy:.2f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Logistic Regression Baseline**—As a final comparison, we trained a multinomial logistic regression model on the same 1 000‐sample MNIST subset with default L2 regularization and a maximum of 300 solver iterations. The model was fitted via an iterative reweighted least squares (IRLS) procedure that minimizes the convex negative log‐likelihood. When evaluated on the 100‐sample test set, this logistic classifier achieved **90 %** accuracy. \n",
"\n",
"This performance lies between the linear‐SVM baseline (88 % cross‐val) and the RBF‐SVM (94 % on test), confirming that while logistic regression captures linear decision boundaries in the high‑dimensional pixel space, kernelized methods—especially with Gaussian kernels—more effectively leverage nonlinear relationships for improved generalization under limited training data."
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAUgAAAFcCAYAAACqUye+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAZbklEQVR4nO3df5jNdf7/8ccMo0bk94+wEjlHzJiwM9NUW9jBhSlhrNEiJVmLaV1Yo/JjtJeyW10XBmv9SD/UtAxq9MOuaiq5ZtiSUiyKJVNMaTAVBq/vH645n04zT95nmC/Tdb9dl6tr3uc57/M+x+nu/X6f875OmHPOCQBQSvil3gAAuFwRSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMFT6QG7dulWjR49W586dFRUVpZtvvlkDBw7U448/Lkk6fPiwoqKiNG7cOHMdRUVFiomJ0R/+8AdJ0qpVq+T3++X3+5WXl1dq3jmnbt26ye/3a8iQIZ6288yZM1qzZo2GDRum+Ph4tWvXTgkJCRo5cqTeeustnTlzphyP/tJ65513NHfu3AtaR15envx+v954442LtFXepKWlqWvXriH9zsGDBzV37lxt37691G1z586V3++/4O1644035Pf79dprr5W67c4775Tf79d7771X6rbExET17dv3gu8fwSp1IHNycpSSkqKioiJNnDhRS5cu1cMPP6yOHTsGXmB169ZV165dtX79eh05cqTM9bz66qs6fvy4kpOTg5ZfddVVWrlyZan5TZs2ad++fbrqqqs8beeJEyc0YsQIpaWlqV69epo+fbqeeeYZpaenq2HDhnrwwQf11ltvhfjoL7133nlHGRkZl3ozyuWPf/xjyNt+6NAhZWRklBnIAQMG6KWXXrrg7YqLi1NYWJhyc3ODlhcWFmrnzp2qXr16qX+0v/76a+3fv1/x8fEXfP8IVvVSb8CFWLx4sZo1a6YlS5aoatX/eyi9e/fWxIkTAz/3799f69atU3Z2tgYPHlxqPVlZWapfv746d+4ctLxXr17Kzs7WtGnTVKNGjcDylStXqkOHDioqKvK0nY899pg2bNigWbNm6a677gq6rXv37ho+fLiOHz/uaV3ncvr0aZ0+fVrVqlUrdduPP/6oyMjIC76PX4rmzZtf1PU1btxYjRs3vuD11K1bV61bt9amTZuClm/evFlVq1ZV//79SwWyJKbnCiR//+VTqfcgCwsLVadOnaA4lggP/7+H9pvf/EaNGzfWqlWrSs19/vnn2rp1q/r06VNqPb1795YkrV27NrDs2LFj+te//qX+/ft72saCggKtXLlSt956a6k4lmjRooXatGkT+Dk/P18TJkxQQkKCoqKi1LNnTy1dujToMPzLL7+U3+/XokWLNH/+fHXt2lXR0dHKzc0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UmZmpFi1aBM71eNGkSRNJ8nxurrCwUA0aNCi1vOTQtbCwMGh5WbM//50S3377rZxzuvnmm9WuXbugPx999JG+++47SQr8w3Ix3nS4GLw+J4WFhYFTBD9Vr169895HbGys5s2bp1OnTmnSpEm67bbblJSUFHT+ORQX+hzGx8dr7969OnjwoPLy8tSuXbvApybi4uK0fft2HTt2THl5eapatao6deoU9PtlPV+hvrZq164d9HPJm38nTpyQpMDrpaznt6y/h8qoUp+DLEtERITGjBmjZcuWBc6Xlejevbtq1aqlrKwsxcXFKScnR3369Dnvx3X69eunOXPmKDMz85yfpyxLfHy8IiIitH79eg0aNOi887Vr11ZBQUGp5SUn7OvUqRO0PJQ9vDp16igsLEzLly8v853ukmV169aVdPbjI5cDr89J7dq19c0335SaK2tZWRITE5WYmKiTJ0/qo48+0sKFCzV+/Hg1bdo05D2inz6HP9/z9SI+Pl5PP/20Nm3apE2bNum2224L3FYSw82bNysvL0/R0dGlXsNlvS5CfW2dT8n8t99+W+o2r8/55a5S70H+9F2+n/r8888lld6DuuKKK5SUlKT3339fixYtUnFxsafD5UaNGmn48OHq0qWL+U60pUGDBkpOTtaGDRu0Zs2aMmf27dunHTt2SJISEhK0e/duffrpp0Eza9asUVhY2AV91q1z585yzungwYOKjo4u9afkXdgOHTqoZs2ayszMlDvHN3KUBPVifETpXBISEpSbm1vqsPfll19WZGSkbrzxRkln9wJ37dql3bt3B829+uqrId1ftWrVFBcXF/io2GeffRZYLnl7vLfccouqVKmiF198MaT7LhEbG6sqVapo3bp12rVrl+Li4gK31axZUzfccIPWrFmjAwcOeH5NXOzX1nXXXacGDRro9ddfD1qen58fOO1R2VXqPcjhw4ercePG6tKli1q2bCnnnLZv366lS5eqevXqGjp0aKnfSU5O1vLly/X000+rZcuWgfM65zNhwoRyb+fkyZO1f/9+paWl6b333lO3bt1Uv359fffdd3r//fe1atUqPfXUU2rTpo2GDRumNWvWaOTIkUpNTVWTJk2Uk5OjF154QYMGDdJ1111X7u3o1KmTBg4cq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bNnieTaUS/cqyvTp0z3PxsbGVtyGwDP2IAHAQCABwEAgAcBAIAHAQCABwEAgAcBAIAHAQCABwEAgAcBAIAHAEOacc5d6I1BxiouLPc8uWLDA8+yUKVM8z4by7YOhCOWlGxYWViHb4PP5PM9+8sknnmcjIiLKszm4yNiDBAADgQQAA4EEAAOBBAADgQQAA4EEAAOBBAADgQQAA4EEAAOBBAADlxqiXLZv3+55dubMmZ5nly9f7nm2oi41vPbaaz3Ppqene54dOnSo51lcHtiDBAADgQQAA4EEAAOBBAADgQQAA4EEAAOBBAADgQQAA4EEAAOBBAADlxoCgIE9SAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADAQSAAwEEgAMBBIADD8P99P+LvQi/uFAAAAAElFTkSuQmCC",
"text/plain": [
"<Figure size 400x400 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"np.random.seed(31)\n",
"svm_pred = y_test_pred\n",
"logistic_pred = logistic_y_pred\n",
"svm_correct_logistic_wrong = X_test[(svm_pred == y_test) & (logistic_pred == y_test)]\n",
"index = np.random.randint(0, len(svm_correct_logistic_wrong))\n",
"image = svm_correct_logistic_wrong[index]\n",
"\n",
"\n",
"show_image(image.reshape((28, 28, 1)), \"SVM Correct, Logistic Wrong\")\n",
"plt.savefig(\"../assets/Q6.png\")\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Example of SVM Correct vs. Logistic Regression Wrong**—To illustrate the practical impact of kernelization, we randomly sampled a test digit for which the RBF‑kernel SVM (with $C=10,\\gamma=\\text{scale}$) produced the correct label while the multinomial logistic regression misclassified it. Figure 8 shows this selected digit—handwritten “6”—which the SVM correctly labels as 6 but which the logistic model erroneously assigns to another class. This case study emphasizes how the nonlinear decision surfaces learned by the RBF‑SVM can capture subtle variations in stroke curvature and width that a purely linear separator cannot reliably distinguish, particularly when training data are limited.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Xet Storage Details

Size:
28.3 kB
·
Xet hash:
82d0a3d916fcc300423f0a40ae218823f391e5fb3b1988eea615ed8e23cf3522

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.