{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f5c4abc0",
   "metadata": {},
   "source": [
    "# Intel® Extension for Scikit-learn NuSVR for Medical Charges dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "27b99b44",
   "metadata": {},
   "outputs": [],
   "source": [
    "from timeit import default_timer as timer\n",
    "from sklearn.datasets import fetch_openml\n",
    "from sklearn.model_selection import train_test_split\n",
    "from IPython.display import HTML\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adf9ffe9",
   "metadata": {},
   "source": [
    "### Download the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a9b315cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = fetch_openml(name=\"medical_charges_nominal\", return_X_y=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49fbf604",
   "metadata": {},
   "source": [
    "### Preprocessing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fafea10b",
   "metadata": {},
   "source": [
    "Encode categorical features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f77c30f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_columns = x.select_dtypes([\"category\"]).columns\n",
    "x[cat_columns] = x[cat_columns].apply(lambda x: x.cat.codes)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd8d3b6d",
   "metadata": {},
   "source": [
    "Split the data into train and test sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "96e14dd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((48919, 11), (114146, 11), (48919,), (114146,))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.3, random_state=42)\n",
    "x_train.shape, x_test.shape, y_train.shape, y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0341cac9",
   "metadata": {},
   "source": [
    "### Patch original Scikit-learn with Intel® Extension for Scikit-learn\n",
    "Intel® Extension for Scikit-learn (previously known as daal4py) contains drop-in replacement functionality for the stock Scikit-learn package. You can take advantage of the performance optimizations of Intel® Extension for Scikit-learn by adding just two lines of code before the usual Scikit-learn imports:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "244c5bc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Intel(R) Extension for Scikit-learn* enabled (https://github.com/uxlfoundation/scikit-learn-intelex)\n"
     ]
    }
   ],
   "source": [
    "from sklearnex import patch_sklearn\n",
    "\n",
    "patch_sklearn()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb14ac8",
   "metadata": {},
   "source": [
    "Intel® Extension for Scikit-learn patching affects performance of specific Scikit-learn functionality. Refer to the [list of supported algorithms and parameters](https://uxlfoundation.github.io/scikit-learn-intelex/latest/algorithms.html) for details. In cases when unsupported parameters are used, the package fallbacks into original Scikit-learn. If the patching does not cover your scenarios, [submit an issue on GitHub](https://github.com/uxlfoundation/scikit-learn-intelex/issues)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "693b4e26",
   "metadata": {},
   "source": [
    "Training of the NuSVR algorithm with Intel® Extension for Scikit-learn for Medical Charges dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e9b8f06b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Intel® extension for Scikit-learn time: 24.69 s'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.svm import NuSVR\n",
    "\n",
    "params = {\n",
    "    \"nu\": 0.4,\n",
    "    \"C\": y_train.mean(),\n",
    "    \"degree\": 2,\n",
    "    \"kernel\": \"poly\",\n",
    "}\n",
    "start = timer()\n",
    "nusvr = NuSVR(**params).fit(x_train, y_train)\n",
    "train_patched = timer() - start\n",
    "f\"Intel® extension for Scikit-learn time: {train_patched:.2f} s\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d01cdabc",
   "metadata": {},
   "source": [
    "Predict and get a result of the NuSVR algorithm with Intel® Extension for Scikit-learn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9ead2a44",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Intel® extension for Scikit-learn R2 score: 0.8635974264586637'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score_opt = nusvr.score(x_test, y_test)\n",
    "f\"Intel® extension for Scikit-learn R2 score: {score_opt}\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd8e7b0b",
   "metadata": {},
   "source": [
    "### Train the same algorithm with original Scikit-learn\n",
    "In order to cancel optimizations, we use *unpatch_sklearn* and reimport the class NuSVR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5bb884d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearnex import unpatch_sklearn\n",
    "\n",
    "unpatch_sklearn()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cfa0dba",
   "metadata": {},
   "source": [
    "Training of the NuSVR algorithm with original Scikit-learn library for Medical Charges dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ae421d8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Original Scikit-learn time: 331.85 s'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.svm import NuSVR\n",
    "\n",
    "start = timer()\n",
    "nusvr = NuSVR(**params).fit(x_train, y_train)\n",
    "train_unpatched = timer() - start\n",
    "f\"Original Scikit-learn time: {train_unpatched:.2f} s\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23b8faa6",
   "metadata": {},
   "source": [
    "Predict and get a result of the NuSVR algorithm with original Scikit-learn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7644999d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Original Scikit-learn R2 score: 0.8636031741516902'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score_original = nusvr.score(x_test, y_test)\n",
    "f\"Original Scikit-learn R2 score: {score_original}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3a704d51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3>Compare R2 score of patched Scikit-learn and original</h3>R2 score of patched Scikit-learn: 0.8635974264586637 <br>R2 score of unpatched Scikit-learn: 0.8636031741516902 <br>Metrics ratio: 0.999993344520726 <br><h3>With Scikit-learn-intelex patching you can:</h3><ul><li>Use your Scikit-learn code for training and prediction with minimal changes (a couple of lines of code);</li><li>Fast execution training and prediction of Scikit-learn models;</li><li>Get the similar quality</li><li>Get speedup in <strong>13.4</strong> times.</li></ul>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "HTML(\n",
    "    f\"<h3>Compare R2 score of patched Scikit-learn and original</h3>\"\n",
    "    f\"R2 score of patched Scikit-learn: {score_opt} <br>\"\n",
    "    f\"R2 score of unpatched Scikit-learn: {score_original} <br>\"\n",
    "    f\"Metrics ratio: {score_opt/score_original} <br>\"\n",
    "    f\"<h3>With Scikit-learn-intelex patching you can:</h3>\"\n",
    "    f\"<ul>\"\n",
    "    f\"<li>Use your Scikit-learn code for training and prediction with minimal changes (a couple of lines of code);</li>\"\n",
    "    f\"<li>Fast execution training and prediction of Scikit-learn models;</li>\"\n",
    "    f\"<li>Get the similar quality</li>\"\n",
    "    f\"<li>Get speedup in <strong>{(train_unpatched/train_patched):.1f}</strong> times.</li>\"\n",
    "    f\"</ul>\"\n",
    ")"
   ]
  }
 ],
 "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.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}