{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "W9_lmFre4Rse" }, "source": [ "# Smooth SVM\n", "Smooth SVMs solve the following optimization problem:\n", "$$\n", " \\min_{\\mathbf{\\beta} \\in \\mathbb{R}^d} \\\n", " C \\sum_{i=1}^n V(y_i \\mathbf{\\beta}^\\intercal \\mathbf{x}_i) + \\frac{1}{2} \\| \\mathbf{\\beta} \\|_2^2\n", "$$\n", "where $\\mathbf{x}_i \\in \\mathbb{R}^d$ is a feature vector, $y_i \\in \\{-1, 1\\}$ is a binary label, and $V(·)$ is the smoothed hinge loss (shown here with a default smoothing parameter τ=1):\n", "$$\n", " V(z) = \\begin{cases}\n", " 0 & \\text{if } z \\ge 1 \\\\\n", " \\frac{(1-z)^2}{2} & \\text{if } 0 < z < 1 \\\\\n", " \\frac{1}{2}- z & \\text{if } z \\le 0\n", " \\end{cases}\n", "$$\n" ] }, { "cell_type": "markdown", "metadata": { "id": "lO5s9H4tbAP4" }, "source": [ "> **Note.** Since the smooth hinge loss is a plq function, we can optimize it using `rehline.plq_Ridge_Classifier`. \n", "> Moreover, this wrapper adapts the `plqERM_Ridge` into a classifier, compatible with the scikit-learn API." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "sRNXaXpO4Pt2" }, "outputs": [], "source": [ "## install rehline\n", "%pip install rehline -q" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "lRz5G7RAaFtj" }, "outputs": [], "source": [ "## set up plotting style\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "custom_palette = [\"#FFE4E1\", \"#3D325C\"]\n", "sns.set_palette(custom_palette)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "ZawpNRuQR9Uu" }, "outputs": [], "source": [ "## simulate data\n", "import numpy as np\n", "from sklearn.datasets import make_classification\n", "from sklearn.preprocessing import StandardScaler\n", "\n", "scaler = StandardScaler()\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\n", "X = scaler.fit_transform(X)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 80 }, "id": "lskTdleDZwpa", "outputId": "3408050f-4e9d-49a3-f702-8158500bf421" }, "outputs": [ { "data": { "text/html": [ "
plq_Ridge_Classifier(loss={'name': 'sSVM'})In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. plq_Ridge_Classifier(loss={'name': 'sSVM'})Pipeline(steps=[('scaler', StandardScaler()),\n",
" ('clf', plq_Ridge_Classifier(loss={'name': 'sSVM'}))])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. Pipeline(steps=[('scaler', StandardScaler()),\n",
" ('clf', plq_Ridge_Classifier(loss={'name': 'sSVM'}))])StandardScaler()
plq_Ridge_Classifier(loss={'name': 'sSVM'})