{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fbcb401d-6ca6-4933-abd5-f8f504282416",
   "metadata": {},
   "source": [
    "# **SVM**\n",
    "\n",
    "[![Slides](https://img.shields.io/badge/🦌-ReHLine-blueviolet)](https://rehline-python.readthedocs.io/en/latest/)\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1f_7t1t6FNxAooQOmpyhHCOVq0IKgMxe-?usp=sharing)\n",
    "\n",
    "SVMs solve the following optimization problem:\n",
    "$$\n",
    "  \\min_{\\mathbf{\\beta} \\in \\mathbb{R}^d} \\ C \\sum_{i=1}^n ( 1 - 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, and $y_i \\in \\{-1, 1\\}$ is a binary label.\n",
    "\n",
    "> **Note.** Since the hinge loss is a plq function, thus we can solve it by `rehline.plqERM_Ridge`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2dd1c096-e0df-492f-be63-8ac272007237",
   "metadata": {},
   "outputs": [],
   "source": [
    "## simulate data\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import numpy as np\n",
    "\n",
    "scaler = StandardScaler()\n",
    "\n",
    "n, d = 10000, 5\n",
    "X, y = make_classification(n_samples=n, n_features=d)\n",
    "## convert y to +1/-1\n",
    "y = 2*y - 1\n",
    "X = scaler.fit_transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aece9fbe-f9be-40ae-8179-b44849fb0fd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "## solve SVM via `plqERM_Ridge`\n",
    "from rehline import plqERM_Ridge\n",
    "\n",
    "clf = plqERM_Ridge(loss={'name': 'svm'}, C=1.0)\n",
    "clf.fit(X=X, y=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "93719987-c6b3-4a9b-9b40-c35e5bf90ef0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18816 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "import warnings\n",
    "import matplotlib.pyplot as plt\n",
    "warnings.filterwarnings(\"ignore\", \"is_categorical_dtype\")\n",
    "warnings.filterwarnings(\"ignore\", \"use_inf_as_na\")\n",
    "\n",
    "score = clf.decision_function(X)\n",
    "df = pd.DataFrame({'score': score, 'y': y})\n",
    "sns.histplot(df, x=\"score\", hue=\"y\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
