{ "cells": [ { "cell_type": "markdown", "source": [ "# Compare SVM Losses with GridSearchCV\n", "\n", "Due to its compatibility with the scikit-learn API, `GridSearchCV` can be used to compare `SVM`, `Smooth SVM`, and `Squared SVM` losses under a unified pipeline, identify the best loss, and select its optimal hyperparameter." ], "metadata": { "id": "LS0oNHbfk_fh" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "WaRThRkBIA5j" }, "outputs": [], "source": [ "## install rehline\n", "%pip install rehline -q" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "O76jxeIHIALp" }, "outputs": [], "source": [ "## set up plotting style\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "custom_palette = ['#FFE4E1', '#3D325C']\n", "sns.set_palette(custom_palette)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "IDityv5qCrQv" }, "outputs": [], "source": [ "## simulate data\n", "from sklearn.datasets import make_classification\n", "import numpy as np\n", "\n", "n, d = 10000, 5\n", "X, y = make_classification(n_samples=n, n_features=d, random_state=42)\n", "y = 2*y - 1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 188 }, "id": "JKowJ-3KHCrF", "outputId": "7ac2550b-91a4-4fec-e5f8-8bb406882c19" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "GridSearchCV(cv=5,\n", " estimator=Pipeline(steps=[('scaler', StandardScaler()),\n", " ('clf',\n", " plq_Ridge_Classifier(loss={'name': 'svm'}))]),\n", " param_grid={'clf__C': [0.1, 1.0, 10.0],\n", " 'clf__loss': [{'name': 'svm'}, {'name': 'sSVM'},\n", " {'name': 'squared SVM'}]})" ], "text/html": [ "
GridSearchCV(cv=5,\n",
" estimator=Pipeline(steps=[('scaler', StandardScaler()),\n",
" ('clf',\n",
" plq_Ridge_Classifier(loss={'name': 'svm'}))]),\n",
" param_grid={'clf__C': [0.1, 1.0, 10.0],\n",
" 'clf__loss': [{'name': 'svm'}, {'name': 'sSVM'},\n",
" {'name': 'squared SVM'}]})In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. GridSearchCV(cv=5,\n",
" estimator=Pipeline(steps=[('scaler', StandardScaler()),\n",
" ('clf',\n",
" plq_Ridge_Classifier(loss={'name': 'svm'}))]),\n",
" param_grid={'clf__C': [0.1, 1.0, 10.0],\n",
" 'clf__loss': [{'name': 'svm'}, {'name': 'sSVM'},\n",
" {'name': 'squared SVM'}]})Pipeline(steps=[('scaler', StandardScaler()),\n",
" ('clf', plq_Ridge_Classifier(C=0.1, loss={'name': 'svm'}))])StandardScaler()
plq_Ridge_Classifier(C=0.1, loss={'name': 'svm'})