{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "tM4LA2VUOg-J" }, "source": [ "# Squared SVM\n", "[](https://rehline-python.readthedocs.io/en/latest/)\n", "\n", "Squared SVMs solve the following optimization problem:\n", "$$\n", " \\min_{\\mathbf{\\beta} \\in \\mathbb{R}^d} \\ C \\sum_{i=1}^n \\left( 1 - y_i \\mathbf{\\beta}^\\intercal \\mathbf{x}_i \\right)_+^2 + \\frac{1}{2} \\| \\mathbf{\\beta} \\|_2^2\n", "$$\n", "where $\\mathbf{x}_i \\in \\mathbb{R}^d$ is a feature vector, and $y_i \\in \\{-1, 1\\}$ is a binary label.\n", "\n", "> **Note.** Since the squared 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.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "rXs8JYXzTbJ5" }, "outputs": [], "source": [ "## install rehline\n", "%pip install rehline -q" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "QvfFVk7WRUAb" }, "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": "w08Rlr8La4Mv" }, "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": "MlOfn-UmRCuN", "outputId": "6d9bb00a-1727-46b2-a26f-933ff00d120e" }, "outputs": [ { "data": { "text/html": [ "
plq_Ridge_Classifier(loss={'name': 'squared SVM'})In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. plq_Ridge_Classifier(loss={'name': 'squared SVM'})Pipeline(steps=[('scaler', StandardScaler()),\n",
" ('clf', plq_Ridge_Classifier(loss={'name': 'squared SVM'}))])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': 'squared SVM'}))])StandardScaler()
plq_Ridge_Classifier(loss={'name': 'squared SVM'})