You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1143 lines
594 KiB
1143 lines
594 KiB
2 years ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"___\n",
|
||
|
"\n",
|
||
|
"<a href='https://www.udemy.com/user/joseportilla/'><img src='../Pierian_Data_Logo.png'/></a>\n",
|
||
|
"___\n",
|
||
|
"<center><em>Copyright by Pierian Data Inc.</em></center>\n",
|
||
|
"<center><em>For more information, visit us at <a href='http://www.pieriandata.com'>www.pieriandata.com</a></em></center>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Exploring Support Vector Machines\n",
|
||
|
"\n",
|
||
|
"**NOTE: For this example, we will explore the algorithm, so we'll skip scaling and also skip a train\\test split and instead see how the various parameters can change an SVM (easiest to visualize the effects in classification)**\n",
|
||
|
"\n",
|
||
|
"[Link to a great Paper on SVM](http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=4448154647BC7B10C991CEF2236BBA38?doi=10.1.1.114.4288&rep=rep1&type=pdf)\n",
|
||
|
"* A tutorial on support vector regression by ALEX J. SMOLA and BERNHARD SCHOLKOPF\n",
|
||
|
"\n",
|
||
|
"## SVM - Classification"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
"import pandas as pd\n",
|
||
|
"import seaborn as sns\n",
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Data\n",
|
||
|
"\n",
|
||
|
"The data shown here simulates a medical study in which mice infected with a virus were given various doses of two medicines and then checked 2 weeks later to see if they were still infected. Given this data, our goal is to create a classifcation model than predict (given two dosage measurements) if they mouse will still be infected with the virus.\n",
|
||
|
"\n",
|
||
|
"You will notice the groups are very separable, this is on purpose, to explore how the various parameters of an SVM model behave."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"df = pd.read_csv(\"../DATA/mouse_viral_study.csv\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<div>\n",
|
||
|
"<style scoped>\n",
|
||
|
" .dataframe tbody tr th:only-of-type {\n",
|
||
|
" vertical-align: middle;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" .dataframe tbody tr th {\n",
|
||
|
" vertical-align: top;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" .dataframe thead th {\n",
|
||
|
" text-align: right;\n",
|
||
|
" }\n",
|
||
|
"</style>\n",
|
||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||
|
" <thead>\n",
|
||
|
" <tr style=\"text-align: right;\">\n",
|
||
|
" <th></th>\n",
|
||
|
" <th>Med_1_mL</th>\n",
|
||
|
" <th>Med_2_mL</th>\n",
|
||
|
" <th>Virus Present</th>\n",
|
||
|
" </tr>\n",
|
||
|
" </thead>\n",
|
||
|
" <tbody>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>0</th>\n",
|
||
|
" <td>6.508231</td>\n",
|
||
|
" <td>8.582531</td>\n",
|
||
|
" <td>0</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>1</th>\n",
|
||
|
" <td>4.126116</td>\n",
|
||
|
" <td>3.073459</td>\n",
|
||
|
" <td>1</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2</th>\n",
|
||
|
" <td>6.427870</td>\n",
|
||
|
" <td>6.369758</td>\n",
|
||
|
" <td>0</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>3</th>\n",
|
||
|
" <td>3.672953</td>\n",
|
||
|
" <td>4.905215</td>\n",
|
||
|
" <td>1</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>4</th>\n",
|
||
|
" <td>1.580321</td>\n",
|
||
|
" <td>2.440562</td>\n",
|
||
|
" <td>1</td>\n",
|
||
|
" </tr>\n",
|
||
|
" </tbody>\n",
|
||
|
"</table>\n",
|
||
|
"</div>"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
" Med_1_mL Med_2_mL Virus Present\n",
|
||
|
"0 6.508231 8.582531 0\n",
|
||
|
"1 4.126116 3.073459 1\n",
|
||
|
"2 6.427870 6.369758 0\n",
|
||
|
"3 3.672953 4.905215 1\n",
|
||
|
"4 1.580321 2.440562 1"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"df.head()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"Index(['Med_1_mL', 'Med_2_mL', 'Virus Present'], dtype='object')"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"df.columns"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Classes"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<AxesSubplot:xlabel='Med_1_mL', ylabel='Med_2_mL'>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAEHCAYAAABGNUbLAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAB2e0lEQVR4nO2dd3hUxf7G38lms5ueEEB6ExAEIiCIFCliBQXsYrl61Z9eQa8N7AXrRUXBctVrxQpW7EpVAVE6IkV6rwlJNsmm787vjzfjOduSTbLJJtn5PE+e5JzdPTt7krwz861CSgmNRqPRRBZR4R6ARqPRaOoeLf4ajUYTgWjx12g0mghEi79Go9FEIFr8NRqNJgKJDvcAgqVp06ayQ4cO4R6GRqPRNChWr16dKaVs5n2+wYh/hw4dsGrVqnAPQ6PRaBoUQog9/s5rs49Go9FEIFr8NRqNJgLR4q/RaDQRSIOx+fujtLQU+/fvR1FRUbiHoinHbrejTZs2sFqt4R6KRqOpgAYt/vv370diYiI6dOgAIUS4hxPxSClx7Ngx7N+/Hx07dgz3cDQaTQXUqtlHCPG2EOKoEGKD6VwTIcR8IcS28u+p1b1+UVER0tLStPDXE4QQSEtL0zuxCCMnB9i/H8jIAPLzwz0aTbDUts1/JoBzvM7dC2ChlLILgIXlx9VGC3/9Qv8+IovMTOCBB4A77wQmTgQ++gjIywv3qDTBUKviL6VcDCDL6/RYAO+W//wugHG1OQaNRlM7FBcDH3/MFb9iwQIgy/s/XlMvCUe0z3FSykMAUP69eaAnCiFuFEKsEkKsyjD/hdUThg8fjrlz53qcmzFjBiZMmICvv/4aU6dODfl7Xn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AkhK4lew9fbi4yn2R8sDsaOjaQJ6/31g2TI+ftllwHHHUchjYyny+flASQl3A99+y/ccN46TgtPJ1+7aBfz8M6+blQVMnQrcfjvw2GPG+yckhPDDa/yibf4aTQThdHIHkJVF004gUlJo4hk5EujShbb5FSuApUtpAsrLA958k3Z7u52mHKeTk8O2bcAjjwArV9J0dPfdXPHn5gLNmvG8mdxcflfBcGlpwMkn18an15jRK3+NJkLIzQXeew9YsoTmmFGjuCoPZKdv2hS49lqu5KUEPv/c9zlHjgAzZvDn++5j0teiRZ7PKSwENm6kuSk7mzuK8hgHAIzwOe444KabaIrq3l3H/dcFeuWv0UQIa9cCixdTyF0u4Jtv/NfmMWOzcXKIjQXK4xI86NQJaNKE9v3sbE4UfiKGER9PU87atbTnmyOQLrkEiIvjxDFoUOXCX1TE99IBZzVDr/w1mgigtBT480/goouAXr0o0kuW8NyJJ1b+eqsVOP98OmV37KCJ5oILDKG22Xhu2TLa71et4nsAdOp26kT7///9H0s8PPYYdwRNmnBSMKXBVEhODjB7Nk1HrVpxt9CqVcVhqxr/aPHXaCIAqxW48EKabh5/nCvvCy8EevcO7vX5+XztOefQRANw5a0ieeLigGuuAd55hw7gKVM4SaSmcsegkslSU1leoqyMk8OuXcCPP3JCUolkgSgqAj78EPjlFx5v2ULfgi4PUT20+NeQH3/8EbfddhtcLhduuOEGv5U8NZpw43YDa9ZwtQ8weufwYf8JYP4oKmKt/gULjHMxMcBLL3HVb7cDQ4fSAZyXx4SuDh38l4+IiuJ4vv4a+PJLnvvuO+CMM4CrruJE4o/CQjqdzeTl0dGsxb/q6M1SDXC5XJg4cSJ++OEHbNq0CbNmzfKokaPR1BcKCyn+AO3qd97JlfdnnzGmX5loAuGvQZv3ubg4Ook7dqQ5p6K6QQUFFHwzCxdWHIFksQAtW/qOwZ+PQVM
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"sns.scatterplot(x='Med_1_mL',y='Med_2_mL',hue='Virus Present',\n",
|
||
|
" data=df,palette='seismic')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Separating Hyperplane\n",
|
||
|
"\n",
|
||
|
"Our goal with SVM is to create the best separating hyperplane. In 2 dimensions, this is simply a line."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[<matplotlib.lines.Line2D at 0x1dab706f208>]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAEHCAYAAABGNUbLAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAACDpUlEQVR4nO2ddZhVVRfG3z3dAQzdIDFDDdKCiDRSEhLWRyMdSkiHCFKCdGMACmKAkgqCgCAdQ3fHMMV07O+Pl+PN6TtzJ/bveeaBe+655+xzB9Zae6WQUkKhUCgUuQsbay9AoVAoFJmPEv4KhUKRC1HCX6FQKHIhSvgrFApFLkQJf4VCociF2Fl7ASklX758smTJktZehkKhUGQrTpw48UxK6WN8PNsI/5IlS+L48ePWXoZCoVBkK4QQt80dV24fhUKhyIUo4a9QKBS5ECX8FQqFIheSbXz+ipxFbGws7t27h6ioKGsvRfESJycnFC1aFPb29tZeiiITUMJfYRXu3bsHd3d3lCxZEkIIay8n1yOlRGBgIO7du4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qW0dfPAghVFgIC3+bt1oQSosy4ULF7FyZUU8eMDXQgBTpgAVKqTvugkJQHQ0dwI2ZkxOKYFly4B9+wyPv/UW8OGH6bt3bscqLZ2FEBsBHAFQXghxTwjRC8BMAE2FEFcBNH35Osfj4eGBpUuXYv/+/bCxsUHjxo3Rt2/f/wZxK5KmQQNgyRLg++/5pxL8GUN8PP4T/ACF8qZNiadnSgkEBQHHjwOnT3PXoM+LF3w/OprWvjnBD1DJlC5terxkSf4
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"sns.scatterplot(x='Med_1_mL',y='Med_2_mL',hue='Virus Present',palette='seismic',data=df)\n",
|
||
|
"\n",
|
||
|
"# We want to somehow automatically create a separating hyperplane ( a line in 2D)\n",
|
||
|
"\n",
|
||
|
"x = np.linspace(0,10,100)\n",
|
||
|
"m = -1\n",
|
||
|
"b = 11\n",
|
||
|
"y = m*x + b\n",
|
||
|
"plt.plot(x,y,'k')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## SVM - Support Vector Machine"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from sklearn.svm import SVC # Supprt Vector Classifier"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Help on class SVC in module sklearn.svm._classes:\n",
|
||
|
"\n",
|
||
|
"class SVC(sklearn.svm._base.BaseSVC)\n",
|
||
|
" | SVC(*, C=1.0, kernel='rbf', degree=3, gamma='scale', coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape='ovr', break_ties=False, random_state=None)\n",
|
||
|
" | \n",
|
||
|
" | C-Support Vector Classification.\n",
|
||
|
" | \n",
|
||
|
" | The implementation is based on libsvm. The fit time scales at least\n",
|
||
|
" | quadratically with the number of samples and may be impractical\n",
|
||
|
" | beyond tens of thousands of samples. For large datasets\n",
|
||
|
" | consider using :class:`sklearn.svm.LinearSVC` or\n",
|
||
|
" | :class:`sklearn.linear_model.SGDClassifier` instead, possibly after a\n",
|
||
|
" | :class:`sklearn.kernel_approximation.Nystroem` transformer.\n",
|
||
|
" | \n",
|
||
|
" | The multiclass support is handled according to a one-vs-one scheme.\n",
|
||
|
" | \n",
|
||
|
" | For details on the precise mathematical formulation of the provided\n",
|
||
|
" | kernel functions and how `gamma`, `coef0` and `degree` affect each\n",
|
||
|
" | other, see the corresponding section in the narrative documentation:\n",
|
||
|
" | :ref:`svm_kernels`.\n",
|
||
|
" | \n",
|
||
|
" | Read more in the :ref:`User Guide <svm_classification>`.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | C : float, default=1.0\n",
|
||
|
" | Regularization parameter. The strength of the regularization is\n",
|
||
|
" | inversely proportional to C. Must be strictly positive. The penalty\n",
|
||
|
" | is a squared l2 penalty.\n",
|
||
|
" | \n",
|
||
|
" | kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'}, default='rbf'\n",
|
||
|
" | Specifies the kernel type to be used in the algorithm.\n",
|
||
|
" | It must be one of 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed' or\n",
|
||
|
" | a callable.\n",
|
||
|
" | If none is given, 'rbf' will be used. If a callable is given it is\n",
|
||
|
" | used to pre-compute the kernel matrix from data matrices; that matrix\n",
|
||
|
" | should be an array of shape ``(n_samples, n_samples)``.\n",
|
||
|
" | \n",
|
||
|
" | degree : int, default=3\n",
|
||
|
" | Degree of the polynomial kernel function ('poly').\n",
|
||
|
" | Ignored by all other kernels.\n",
|
||
|
" | \n",
|
||
|
" | gamma : {'scale', 'auto'} or float, default='scale'\n",
|
||
|
" | Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.\n",
|
||
|
" | \n",
|
||
|
" | - if ``gamma='scale'`` (default) is passed then it uses\n",
|
||
|
" | 1 / (n_features * X.var()) as value of gamma,\n",
|
||
|
" | - if 'auto', uses 1 / n_features.\n",
|
||
|
" | \n",
|
||
|
" | .. versionchanged:: 0.22\n",
|
||
|
" | The default value of ``gamma`` changed from 'auto' to 'scale'.\n",
|
||
|
" | \n",
|
||
|
" | coef0 : float, default=0.0\n",
|
||
|
" | Independent term in kernel function.\n",
|
||
|
" | It is only significant in 'poly' and 'sigmoid'.\n",
|
||
|
" | \n",
|
||
|
" | shrinking : bool, default=True\n",
|
||
|
" | Whether to use the shrinking heuristic.\n",
|
||
|
" | See the :ref:`User Guide <shrinking_svm>`.\n",
|
||
|
" | \n",
|
||
|
" | probability : bool, default=False\n",
|
||
|
" | Whether to enable probability estimates. This must be enabled prior\n",
|
||
|
" | to calling `fit`, will slow down that method as it internally uses\n",
|
||
|
" | 5-fold cross-validation, and `predict_proba` may be inconsistent with\n",
|
||
|
" | `predict`. Read more in the :ref:`User Guide <scores_probabilities>`.\n",
|
||
|
" | \n",
|
||
|
" | tol : float, default=1e-3\n",
|
||
|
" | Tolerance for stopping criterion.\n",
|
||
|
" | \n",
|
||
|
" | cache_size : float, default=200\n",
|
||
|
" | Specify the size of the kernel cache (in MB).\n",
|
||
|
" | \n",
|
||
|
" | class_weight : dict or 'balanced', default=None\n",
|
||
|
" | Set the parameter C of class i to class_weight[i]*C for\n",
|
||
|
" | SVC. If not given, all classes are supposed to have\n",
|
||
|
" | weight one.\n",
|
||
|
" | The \"balanced\" mode uses the values of y to automatically adjust\n",
|
||
|
" | weights inversely proportional to class frequencies in the input data\n",
|
||
|
" | as ``n_samples / (n_classes * np.bincount(y))``\n",
|
||
|
" | \n",
|
||
|
" | verbose : bool, default=False\n",
|
||
|
" | Enable verbose output. Note that this setting takes advantage of a\n",
|
||
|
" | per-process runtime setting in libsvm that, if enabled, may not work\n",
|
||
|
" | properly in a multithreaded context.\n",
|
||
|
" | \n",
|
||
|
" | max_iter : int, default=-1\n",
|
||
|
" | Hard limit on iterations within solver, or -1 for no limit.\n",
|
||
|
" | \n",
|
||
|
" | decision_function_shape : {'ovo', 'ovr'}, default='ovr'\n",
|
||
|
" | Whether to return a one-vs-rest ('ovr') decision function of shape\n",
|
||
|
" | (n_samples, n_classes) as all other classifiers, or the original\n",
|
||
|
" | one-vs-one ('ovo') decision function of libsvm which has shape\n",
|
||
|
" | (n_samples, n_classes * (n_classes - 1) / 2). However, one-vs-one\n",
|
||
|
" | ('ovo') is always used as multi-class strategy. The parameter is\n",
|
||
|
" | ignored for binary classification.\n",
|
||
|
" | \n",
|
||
|
" | .. versionchanged:: 0.19\n",
|
||
|
" | decision_function_shape is 'ovr' by default.\n",
|
||
|
" | \n",
|
||
|
" | .. versionadded:: 0.17\n",
|
||
|
" | *decision_function_shape='ovr'* is recommended.\n",
|
||
|
" | \n",
|
||
|
" | .. versionchanged:: 0.17\n",
|
||
|
" | Deprecated *decision_function_shape='ovo' and None*.\n",
|
||
|
" | \n",
|
||
|
" | break_ties : bool, default=False\n",
|
||
|
" | If true, ``decision_function_shape='ovr'``, and number of classes > 2,\n",
|
||
|
" | :term:`predict` will break ties according to the confidence values of\n",
|
||
|
" | :term:`decision_function`; otherwise the first class among the tied\n",
|
||
|
" | classes is returned. Please note that breaking ties comes at a\n",
|
||
|
" | relatively high computational cost compared to a simple predict.\n",
|
||
|
" | \n",
|
||
|
" | .. versionadded:: 0.22\n",
|
||
|
" | \n",
|
||
|
" | random_state : int or RandomState instance, default=None\n",
|
||
|
" | Controls the pseudo random number generation for shuffling the data for\n",
|
||
|
" | probability estimates. Ignored when `probability` is False.\n",
|
||
|
" | Pass an int for reproducible output across multiple function calls.\n",
|
||
|
" | See :term:`Glossary <random_state>`.\n",
|
||
|
" | \n",
|
||
|
" | Attributes\n",
|
||
|
" | ----------\n",
|
||
|
" | support_ : ndarray of shape (n_SV,)\n",
|
||
|
" | Indices of support vectors.\n",
|
||
|
" | \n",
|
||
|
" | support_vectors_ : ndarray of shape (n_SV, n_features)\n",
|
||
|
" | Support vectors.\n",
|
||
|
" | \n",
|
||
|
" | n_support_ : ndarray of shape (n_class,), dtype=int32\n",
|
||
|
" | Number of support vectors for each class.\n",
|
||
|
" | \n",
|
||
|
" | dual_coef_ : ndarray of shape (n_class-1, n_SV)\n",
|
||
|
" | Dual coefficients of the support vector in the decision\n",
|
||
|
" | function (see :ref:`sgd_mathematical_formulation`), multiplied by\n",
|
||
|
" | their targets.\n",
|
||
|
" | For multiclass, coefficient for all 1-vs-1 classifiers.\n",
|
||
|
" | The layout of the coefficients in the multiclass case is somewhat\n",
|
||
|
" | non-trivial. See the :ref:`multi-class section of the User Guide\n",
|
||
|
" | <svm_multi_class>` for details.\n",
|
||
|
" | \n",
|
||
|
" | coef_ : ndarray of shape (n_class * (n_class-1) / 2, n_features)\n",
|
||
|
" | Weights assigned to the features (coefficients in the primal\n",
|
||
|
" | problem). This is only available in the case of a linear kernel.\n",
|
||
|
" | \n",
|
||
|
" | `coef_` is a readonly property derived from `dual_coef_` and\n",
|
||
|
" | `support_vectors_`.\n",
|
||
|
" | \n",
|
||
|
" | intercept_ : ndarray of shape (n_class * (n_class-1) / 2,)\n",
|
||
|
" | Constants in decision function.\n",
|
||
|
" | \n",
|
||
|
" | fit_status_ : int\n",
|
||
|
" | 0 if correctly fitted, 1 otherwise (will raise warning)\n",
|
||
|
" | \n",
|
||
|
" | classes_ : ndarray of shape (n_classes,)\n",
|
||
|
" | The classes labels.\n",
|
||
|
" | \n",
|
||
|
" | probA_ : ndarray of shape (n_class * (n_class-1) / 2)\n",
|
||
|
" | probB_ : ndarray of shape (n_class * (n_class-1) / 2)\n",
|
||
|
" | If `probability=True`, it corresponds to the parameters learned in\n",
|
||
|
" | Platt scaling to produce probability estimates from decision values.\n",
|
||
|
" | If `probability=False`, it's an empty array. Platt scaling uses the\n",
|
||
|
" | logistic function\n",
|
||
|
" | ``1 / (1 + exp(decision_value * probA_ + probB_))``\n",
|
||
|
" | where ``probA_`` and ``probB_`` are learned from the dataset [2]_. For\n",
|
||
|
" | more information on the multiclass case and training procedure see\n",
|
||
|
" | section 8 of [1]_.\n",
|
||
|
" | \n",
|
||
|
" | class_weight_ : ndarray of shape (n_class,)\n",
|
||
|
" | Multipliers of parameter C for each class.\n",
|
||
|
" | Computed based on the ``class_weight`` parameter.\n",
|
||
|
" | \n",
|
||
|
" | shape_fit_ : tuple of int of shape (n_dimensions_of_X,)\n",
|
||
|
" | Array dimensions of training vector ``X``.\n",
|
||
|
" | \n",
|
||
|
" | Examples\n",
|
||
|
" | --------\n",
|
||
|
" | >>> import numpy as np\n",
|
||
|
" | >>> from sklearn.pipeline import make_pipeline\n",
|
||
|
" | >>> from sklearn.preprocessing import StandardScaler\n",
|
||
|
" | >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])\n",
|
||
|
" | >>> y = np.array([1, 1, 2, 2])\n",
|
||
|
" | >>> from sklearn.svm import SVC\n",
|
||
|
" | >>> clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))\n",
|
||
|
" | >>> clf.fit(X, y)\n",
|
||
|
" | Pipeline(steps=[('standardscaler', StandardScaler()),\n",
|
||
|
" | ('svc', SVC(gamma='auto'))])\n",
|
||
|
" | \n",
|
||
|
" | >>> print(clf.predict([[-0.8, -1]]))\n",
|
||
|
" | [1]\n",
|
||
|
" | \n",
|
||
|
" | See also\n",
|
||
|
" | --------\n",
|
||
|
" | SVR\n",
|
||
|
" | Support Vector Machine for Regression implemented using libsvm.\n",
|
||
|
" | \n",
|
||
|
" | LinearSVC\n",
|
||
|
" | Scalable Linear Support Vector Machine for classification\n",
|
||
|
" | implemented using liblinear. Check the See also section of\n",
|
||
|
" | LinearSVC for more comparison element.\n",
|
||
|
" | \n",
|
||
|
" | References\n",
|
||
|
" | ----------\n",
|
||
|
" | .. [1] `LIBSVM: A Library for Support Vector Machines\n",
|
||
|
" | <http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf>`_\n",
|
||
|
" | \n",
|
||
|
" | .. [2] `Platt, John (1999). \"Probabilistic outputs for support vector\n",
|
||
|
" | machines and comparison to regularizedlikelihood methods.\"\n",
|
||
|
" | <http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.1639>`_\n",
|
||
|
" | \n",
|
||
|
" | Method resolution order:\n",
|
||
|
" | SVC\n",
|
||
|
" | sklearn.svm._base.BaseSVC\n",
|
||
|
" | sklearn.base.ClassifierMixin\n",
|
||
|
" | sklearn.svm._base.BaseLibSVM\n",
|
||
|
" | sklearn.base.BaseEstimator\n",
|
||
|
" | builtins.object\n",
|
||
|
" | \n",
|
||
|
" | Methods defined here:\n",
|
||
|
" | \n",
|
||
|
" | __init__(self, *, C=1.0, kernel='rbf', degree=3, gamma='scale', coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape='ovr', break_ties=False, random_state=None)\n",
|
||
|
" | Initialize self. See help(type(self)) for accurate signature.\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Data and other attributes defined here:\n",
|
||
|
" | \n",
|
||
|
" | __abstractmethods__ = frozenset()\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Methods inherited from sklearn.svm._base.BaseSVC:\n",
|
||
|
" | \n",
|
||
|
" | decision_function(self, X)\n",
|
||
|
" | Evaluates the decision function for the samples in X.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | X : array-like of shape (n_samples, n_features)\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | X : ndarray of shape (n_samples, n_classes * (n_classes-1) / 2)\n",
|
||
|
" | Returns the decision function of the sample for each class\n",
|
||
|
" | in the model.\n",
|
||
|
" | If decision_function_shape='ovr', the shape is (n_samples,\n",
|
||
|
" | n_classes).\n",
|
||
|
" | \n",
|
||
|
" | Notes\n",
|
||
|
" | -----\n",
|
||
|
" | If decision_function_shape='ovo', the function values are proportional\n",
|
||
|
" | to the distance of the samples X to the separating hyperplane. If the\n",
|
||
|
" | exact distances are required, divide the function values by the norm of\n",
|
||
|
" | the weight vector (``coef_``). See also `this question\n",
|
||
|
" | <https://stats.stackexchange.com/questions/14876/\n",
|
||
|
" | interpreting-distance-from-hyperplane-in-svm>`_ for further details.\n",
|
||
|
" | If decision_function_shape='ovr', the decision function is a monotonic\n",
|
||
|
" | transformation of ovo decision function.\n",
|
||
|
" | \n",
|
||
|
" | predict(self, X)\n",
|
||
|
" | Perform classification on samples in X.\n",
|
||
|
" | \n",
|
||
|
" | For an one-class model, +1 or -1 is returned.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | X : {array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples_test, n_samples_train)\n",
|
||
|
" | For kernel=\"precomputed\", the expected shape of X is\n",
|
||
|
" | (n_samples_test, n_samples_train).\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | y_pred : ndarray of shape (n_samples,)\n",
|
||
|
" | Class labels for samples in X.\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Data descriptors inherited from sklearn.svm._base.BaseSVC:\n",
|
||
|
" | \n",
|
||
|
" | predict_log_proba\n",
|
||
|
" | Compute log probabilities of possible outcomes for samples in X.\n",
|
||
|
" | \n",
|
||
|
" | The model need to have probability information computed at training\n",
|
||
|
" | time: fit with attribute `probability` set to True.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | X : array-like of shape (n_samples, n_features) or (n_samples_test, n_samples_train)\n",
|
||
|
" | For kernel=\"precomputed\", the expected shape of X is\n",
|
||
|
" | (n_samples_test, n_samples_train).\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | T : ndarray of shape (n_samples, n_classes)\n",
|
||
|
" | Returns the log-probabilities of the sample for each class in\n",
|
||
|
" | the model. The columns correspond to the classes in sorted\n",
|
||
|
" | order, as they appear in the attribute :term:`classes_`.\n",
|
||
|
" | \n",
|
||
|
" | Notes\n",
|
||
|
" | -----\n",
|
||
|
" | The probability model is created using cross validation, so\n",
|
||
|
" | the results can be slightly different than those obtained by\n",
|
||
|
" | predict. Also, it will produce meaningless results on very small\n",
|
||
|
" | datasets.\n",
|
||
|
" | \n",
|
||
|
" | predict_proba\n",
|
||
|
" | Compute probabilities of possible outcomes for samples in X.\n",
|
||
|
" | \n",
|
||
|
" | The model need to have probability information computed at training\n",
|
||
|
" | time: fit with attribute `probability` set to True.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | X : array-like of shape (n_samples, n_features)\n",
|
||
|
" | For kernel=\"precomputed\", the expected shape of X is\n",
|
||
|
" | [n_samples_test, n_samples_train]\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | T : ndarray of shape (n_samples, n_classes)\n",
|
||
|
" | Returns the probability of the sample for each class in\n",
|
||
|
" | the model. The columns correspond to the classes in sorted\n",
|
||
|
" | order, as they appear in the attribute :term:`classes_`.\n",
|
||
|
" | \n",
|
||
|
" | Notes\n",
|
||
|
" | -----\n",
|
||
|
" | The probability model is created using cross validation, so\n",
|
||
|
" | the results can be slightly different than those obtained by\n",
|
||
|
" | predict. Also, it will produce meaningless results on very small\n",
|
||
|
" | datasets.\n",
|
||
|
" | \n",
|
||
|
" | probA_\n",
|
||
|
" | \n",
|
||
|
" | probB_\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Methods inherited from sklearn.base.ClassifierMixin:\n",
|
||
|
" | \n",
|
||
|
" | score(self, X, y, sample_weight=None)\n",
|
||
|
" | Return the mean accuracy on the given test data and labels.\n",
|
||
|
" | \n",
|
||
|
" | In multi-label classification, this is the subset accuracy\n",
|
||
|
" | which is a harsh metric since you require for each sample that\n",
|
||
|
" | each label set be correctly predicted.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | X : array-like of shape (n_samples, n_features)\n",
|
||
|
" | Test samples.\n",
|
||
|
" | \n",
|
||
|
" | y : array-like of shape (n_samples,) or (n_samples, n_outputs)\n",
|
||
|
" | True labels for X.\n",
|
||
|
" | \n",
|
||
|
" | sample_weight : array-like of shape (n_samples,), default=None\n",
|
||
|
" | Sample weights.\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | score : float\n",
|
||
|
" | Mean accuracy of self.predict(X) wrt. y.\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Data descriptors inherited from sklearn.base.ClassifierMixin:\n",
|
||
|
" | \n",
|
||
|
" | __dict__\n",
|
||
|
" | dictionary for instance variables (if defined)\n",
|
||
|
" | \n",
|
||
|
" | __weakref__\n",
|
||
|
" | list of weak references to the object (if defined)\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Methods inherited from sklearn.svm._base.BaseLibSVM:\n",
|
||
|
" | \n",
|
||
|
" | fit(self, X, y, sample_weight=None)\n",
|
||
|
" | Fit the SVM model according to the given training data.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | X : {array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples, n_samples)\n",
|
||
|
" | Training vectors, where n_samples is the number of samples\n",
|
||
|
" | and n_features is the number of features.\n",
|
||
|
" | For kernel=\"precomputed\", the expected shape of X is\n",
|
||
|
" | (n_samples, n_samples).\n",
|
||
|
" | \n",
|
||
|
" | y : array-like of shape (n_samples,)\n",
|
||
|
" | Target values (class labels in classification, real numbers in\n",
|
||
|
" | regression)\n",
|
||
|
" | \n",
|
||
|
" | sample_weight : array-like of shape (n_samples,), default=None\n",
|
||
|
" | Per-sample weights. Rescale C per sample. Higher weights\n",
|
||
|
" | force the classifier to put more emphasis on these points.\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | self : object\n",
|
||
|
" | \n",
|
||
|
" | Notes\n",
|
||
|
" | -----\n",
|
||
|
" | If X and y are not C-ordered and contiguous arrays of np.float64 and\n",
|
||
|
" | X is not a scipy.sparse.csr_matrix, X and/or y may be copied.\n",
|
||
|
" | \n",
|
||
|
" | If X is a dense array, then the other methods will not support sparse\n",
|
||
|
" | matrices as input.\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Data descriptors inherited from sklearn.svm._base.BaseLibSVM:\n",
|
||
|
" | \n",
|
||
|
" | coef_\n",
|
||
|
" | \n",
|
||
|
" | n_support_\n",
|
||
|
" | \n",
|
||
|
" | ----------------------------------------------------------------------\n",
|
||
|
" | Methods inherited from sklearn.base.BaseEstimator:\n",
|
||
|
" | \n",
|
||
|
" | __getstate__(self)\n",
|
||
|
" | \n",
|
||
|
" | __repr__(self, N_CHAR_MAX=700)\n",
|
||
|
" | Return repr(self).\n",
|
||
|
" | \n",
|
||
|
" | __setstate__(self, state)\n",
|
||
|
" | \n",
|
||
|
" | get_params(self, deep=True)\n",
|
||
|
" | Get parameters for this estimator.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | deep : bool, default=True\n",
|
||
|
" | If True, will return the parameters for this estimator and\n",
|
||
|
" | contained subobjects that are estimators.\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | params : mapping of string to any\n",
|
||
|
" | Parameter names mapped to their values.\n",
|
||
|
" | \n",
|
||
|
" | set_params(self, **params)\n",
|
||
|
" | Set the parameters of this estimator.\n",
|
||
|
" | \n",
|
||
|
" | The method works on simple estimators as well as on nested objects\n",
|
||
|
" | (such as pipelines). The latter have parameters of the form\n",
|
||
|
" | ``<component>__<parameter>`` so that it's possible to update each\n",
|
||
|
" | component of a nested object.\n",
|
||
|
" | \n",
|
||
|
" | Parameters\n",
|
||
|
" | ----------\n",
|
||
|
" | **params : dict\n",
|
||
|
" | Estimator parameters.\n",
|
||
|
" | \n",
|
||
|
" | Returns\n",
|
||
|
" | -------\n",
|
||
|
" | self : object\n",
|
||
|
" | Estimator instance.\n",
|
||
|
"\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"help(SVC)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"**NOTE: For this example, we will explore the algorithm, so we'll skip any scaling or even a train\\test split for now**"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y = df['Virus Present']\n",
|
||
|
"X = df.drop('Virus Present',axis=1) "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"SVC(C=1000, kernel='linear')"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 10,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = SVC(kernel='linear', C=1000)\n",
|
||
|
"model.fit(X, y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# This is imported from the supplemental .py file\n",
|
||
|
"# https://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html\n",
|
||
|
"from svm_margin_plot import plot_svm_boundary"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAC1C0lEQVR4nOyddXgU1/eH31nfjQBBEgKB4E5wd3eSIG1xl0CLtNBvafuru9CWluBapEiCu2twd4JDEiLEk9X5/bFky7IbgWRTaPd9nj40s3Pn3omcOXPuOZ8jiKKIEydOnDh5/ZD80wtw4sSJEycvh9OAO3HixMlritOAO3HixMlritOAO3HixMlritOAO3HixMlriiw/JytSpIjo6+ub7XlGo5Ho6Gg0Gg1ubm4IguD4xQExMTEIgkDBggWRSqX5MqcTJ06cZMepU6diRFEs+vzxfDXgvr6+nDx5MtvzdDodO3fu5MSJExQrVoyAgACKFy/u0LWJosipU6fYvn07UqmUrl27UqNGDYfO6cSJEyc5QRCEu3aP52ceeL169cScGPAMbt68yfr160lNTaVVq1Y0bdoUicSxUZ/Y2FhCQ0N58OAB1atXp1u3bqhUKofO6cSJEydZIQjCKVEU6z1//JWOgZcvX56xY8dSuXJldu/ezcKFC4mLi3PonIULF2bYsGG0adOGx48fO0MpTpw4eWV5pT3wDERR5OLFi2zevBmj0UjHjh2pW7euw2PjRqMRqVSKTqfj8OHDNG3aFIVC4dA5nThx4uR5XksPPANBEKhRowZBQU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y9rbO
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plot_svm_boundary(model,X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Hyper Parameters\n",
|
||
|
"\n",
|
||
|
"### C\n",
|
||
|
"\n",
|
||
|
"Regularization parameter. The strength of the regularization is **inversely** proportional to C. Must be strictly positive. The penalty is a squared l2 penalty.\n",
|
||
|
"\n",
|
||
|
"*Note: If you are following along with the equations, specifically the value of C as described in ISLR, C in scikit-learn is **inversely** proportional to this value.*"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"SVC(C=0.05, kernel='linear')"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 13,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = SVC(kernel='linear', C=0.05)\n",
|
||
|
"model.fit(X, y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAC7DElEQVR4nOydd1gU5xaH39nOUlQQsKHYe29g79h7F7vGWGKLJ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ST4UKJdLKV06P9evz836bqQDmdaBQ/xr35MnTyhcuLDh58z6pfhQK169enXc3Nwydf0RnU7Hpk2bKFKkSJZW/AswZMhiPDxOGMIfFhYKmjevwvbtideTu3fvOQ8fvuLt23eMHbvGbI2TMmUKULRobvbtu4pGYz5ByMpKha2tFc+fB5r93MJCgVarQxRFmjevyooVw7C3z3wb918jmbqhgyAIBuf98uVL1q1b95/Qil++fPk/UVdcoVBw6NAhDh8+nKUX/4wEBYWxatUxo9h1dHQc+/d74eubeHXPUqXy066dKw0bljerBbeyUnH16ix8fQMSdd4AWq2Onj3rY2GhMPlMpZKzePFgYmJ2EBOzg927JybqvDUaLevWnaRjx+mMGLEMb+9XSd12FkmQKRz4h0RERPD8+XNWrVqVqeuKN23aFDc3N+7du5ep64onPJCqV6/OxYsX2bFjR6Z+IH3NvHgRiFwuNTmuUEjx8Um+PHPBgo7kyWNrdEwqlfDrr91RqRSULp0/0bdBpVJGnTqlmTKlO5061USplCGXSxEEyJ07B6NHt6Vq1aJIJBKkUlMbE9Dr9TRv/guDBy9ix44
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plot_svm_boundary(model,X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Kernel\n",
|
||
|
"\n",
|
||
|
"[Choosing a Kernel](https://stats.stackexchange.com/questions/18030/how-to-select-kernel-for-svm?rq=1)\n",
|
||
|
"\n",
|
||
|
"#### rbf - [Radial Basis Function](https://en.wikipedia.org/wiki/Radial_basis_function_kernel)\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"When training an SVM with the Radial Basis Function (RBF) kernel, two parameters must be considered: C and gamma. The parameter C, common to all SVM kernels, trades off misclassification of training examples against simplicity of the decision surface. A low C makes the decision surface smooth, while a high C aims at classifying all training examples correctly. gamma defines how much influence a single training example has. The larger gamma is, the closer other examples must be to be affected."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAADD/klEQVR4nOydd1gU5xaH39m+SxERAREFxV4g9h41dmPsvfceNcb03MT0RKMx9hI1iL1i7xo79l4RBUUpgtLLtrl/bNy47lIUUEx4n8fn3szOfN83C5w5c75zfkcQRZECCiiggALePCSvewEFFFBAAQW8HAUGvIACCijgDaXAgBdQQAEFvKEUGPACCiiggDeUAgNeQAEFFPCGInuVk7m4uIje3t6vcsrXjsFg4PHjxxQqVAiFQvG6l1NAAQW8gZw9ezZGFMWizx9/pQbc29ubM2fOvMopXzvp6eksXLgQrVbLiBEjsLe3f91LKqCAAt4wBEEIs3W8IISSxyiVSrp3705qaiobN27EaDS+7iUVUEAB/xIKDPgrwM3NjbZt23Lnzh2OHDnyupdTQAEF/EsoMOCviGrVquHr68tff/3F3bt3X/dyCiiggH8BBQb8FSEIAu3ataNIkSJs2LCBpKSk172kAgoo4A3nlW5i/tdRKBR0796dRYsWsWHDBvr164dEUvAMLeDl0el0pKSkYGdnh0wmIzo6mjt37pCWlobBYMBgMGA0GmnUqBF2dnbcunWLy5cvm48/Padr165oNBquXbvGjRs3kMlkyGQypFIpMpmMxo0bI5PJCAsL49GjR6jVaot/hQoVQhCE1/11/OfI0oALgrAEaAdEi6JY5e9jzsAawBsIBbqLovgk75b578HV1ZW2bduyefNm9uzZQ6tWrQp+8Q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{
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = SVC(kernel='rbf', C=1)\n",
|
||
|
"model.fit(X, y)\n",
|
||
|
"plot_svm_boundary(model,X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOxdZXgUZxc9s74b3bg7EkJwJ8GDQ7DgQYpDkZYW2n7QYAUKFaS4u0OLO8WluAQnJEhCCBCXze6e78dsQoK0lEJD25znmSfJ7sw7dyRn3rn33HsFkihEIQpRiEL88yApaAMKUYhCFKIQb4dCAi9EIQpRiH8oCgm8EIUoRCH+oSgk8EIUohCF+IeikMALUYhCFOIfCtnfuTM7Ozt6eXn9nbsESTx58gQqlQrm5uZ/674LUYh/I7Kzs/Hs2TM4ODgUtCn/GZw5cyaBpP2Ln/+tBO7l5YXTp0//nbuE0WjEpk2bcOnSJTRt2hQVKlT4W/dfiEL825CWlobk5GQ4OzvDaDQCACSSwpf59wlBEKJf9fnfSuAFAYlEghYtWiArKwvbtm2DUqlEYGBgQZtViEL8Y2FmZgYzMzMAwIEDB3D//n2EhYVBo9EUsGX/PfwnHptSqRRhYWHw9PTEpk2bcOPGjYI2qRCF+FfA1tYW9+7dw9y5cxEXF1fQ5vzn8J8gcACQy+Xo0KEDnJ2dsXbtWty5c6egTSpEIT4YPH78GEePHsWJEyeQlpb2xtuVKVMG3bt3h9FoxIIFC3D58uX3aGUhXsR/hsABQKlUolOnTrC1tcXKlSt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|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = SVC(kernel='sigmoid')\n",
|
||
|
"model.fit(X, y)\n",
|
||
|
"plot_svm_boundary(model,X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Degree (poly kernels only)\n",
|
||
|
"\n",
|
||
|
"Degree of the polynomial kernel function ('poly').\n",
|
||
|
"Ignored by all other kernels."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAC4lklEQVR4nOydd1gUVxeH39nKLkUBQaxg77333js27BWNJWpM1MSYT2MSk5hojBo1Yjf2hthi7733il0RKdJh63x/IBvXXZqisAnv8/AkDnPvnFngzJ1zz/kdQRRFsskmm2yysT0kmW1ANtlkk00270a2A88mm2yysVGyHXg22WSTjY2S7cCzySabbGyUbAeeTTbZZGOjyD7mxXLlyiV6eXl9zEumiCiKhIaGIpfLyZkzZ2abk24MBgOhoaHY29vj4OCQ2eZkk002H4jz58+HiqLo9vbxj+rAvby8OHfu3Me8ZIqIosjBgwc5cuQInp6edO/eHbVandlmpRmdTsfWrVu5du0a5cuXp3379shkH/VHmk022XwEBEF4ZO34fzqEIggCjRs3pnPnzjx79oyFCxfy8uXLzDYrzcjlcjp37kzjxo25cu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m9e83REqVTC1Kk9sbNTUKZMwWT3apRKGfXrl2HKlJ507VoHpVKGXC5FECBPHmfGju1ItWrFkEgkKYbajEYjrVt/y7Bh89i06QTz5u2kUqUxHDt2I1X7s7HEJhw4gJ2dHXK5HL1ez4oVK1i1ahUJCbaTouT
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = SVC(kernel='poly', C=1,degree=1)\n",
|
||
|
"model.fit(X, y)\n",
|
||
|
"plot_svm_boundary(model,X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 18,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAC3U0lEQVR4nOyddXgU5xaH31nfjUCChWAhuDsECe5upRR3bYHSQv3WvVSgEFyLQyDB3d1dkkAIECEhCfHVuX+EbFl2I0A2SPd9nvvcMrsz3zchnPnmfL/zO4Ioijhw4MCBg9cPycuegAMHDhw4eD4cAdyBAwcOXlMcAdyBAwcOXlMcAdyBAwcOXlMcAdyBAwcOXlNkeTlYwYIFRS8vL4tj8fHx6HQ68ufPj0KhyLO5REdHI5FIyJ8/P1KpNM/GdeDAgYNn5cyZMzGiKBZ6+nieBnAvLy9Onz5tcezWrVts2rSJuLg4ateuTZs2bVCr1XadhyiKnDp1it27dwPQsmVL6tevj0TieCFx4MDBq4cgCHdsHs9LHXjdunXFpwM4gF6v58CBAxw9ehS1Wk2HDh2o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IZn82ePY6BA1sgimKW/370egOrVh0iIOAExYq5M358J8qXL5bl3P/rZBbA81RGmBm1atWiVKlSbNq0iS1btnDp0iW6dOlCbnqnZIYgCNSqVYuyZcuydetWdu/ezZUrV+jatStFixa169jt27fH1dWV/fv3c/v2bTp06EC1atUcr5wObHL3bgxyuXX+WqGQEhISmW0AL126CJ6e7hYpEKlUwjff9EO
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = SVC(kernel='poly', C=1,degree=2)\n",
|
||
|
"model.fit(X, y)\n",
|
||
|
"plot_svm_boundary(model,X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### gamma\n",
|
||
|
"\n",
|
||
|
"gamma : {'scale', 'auto'} or float, default='scale'\n",
|
||
|
" Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.\n",
|
||
|
"\n",
|
||
|
" - if ``gamma='scale'`` (default) is passed then it uses\n",
|
||
|
" 1 / (n_features * X.var()) as value of gamma,\n",
|
||
|
" - if 'auto', uses 1 / n_features."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 19,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAADDT0lEQVR4nOyddVhU6RfHP3eSGUoRsLG7FRW7xe7uwnaN1Q3XXeNn7LrqrmsndmBid2Bid2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0714Hlcp0r8fKSs6CBYNITNxCYuIWvL1/SdV5a7U6Vq8+Srt2vzN8+GLu3bOk5L4vFgeegahUKrp160bZsmU5fvw4mzZtQqMx3UiyYIqtrS2DBg2iUKFCQHJoxYIxz56FIpdLTY4rFFIePEjdgb8iX76s5MhhnI4olUqYNKkLVlYKSpRwSVWzRqmUUbNmCSZM6EL79tVQKmXI5VIEAbJnz8yoUa2oWLEQEokEqdTUxlcYDAaaNJnIoEHz2bLlNPPn76FcuRGcPHnrrfZbMMXiwDMYqVRKy5YtadiwIbdv38bT05OoqKjPbdZXwSvnce3aNebNm0dEhKUY5HUKF85pNrMkLi6RMmXyvfX6deuOmxTYyGTSlPj52LHtUankJjFruVxK27ZV2bTpJ2QyKStXjuTx42WcPPkHY8a0ISIilrlzd+PqOoqGDX8jMTH1Scvhw1c5e/YucXHJqwidzkB8fBJDhix8q/0WTLE48I+AIAhUrVqVLl26EB4ezpIlS/D39//cZn01ZM2alaSkJFauXGmRLXiNsLBosxuCoki
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = SVC(kernel='rbf', C=1,gamma=0.01)\n",
|
||
|
"model.fit(X, y)\n",
|
||
|
"plot_svm_boundary(model,X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Grid Search\n",
|
||
|
"\n",
|
||
|
"Keep in mind, for this simple example, we saw the classes were easily separated, which means each variation of model could easily get 100% accuracy, meaning a grid search is \"useless\"."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from sklearn.model_selection import GridSearchCV"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 21,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"svm = SVC()\n",
|
||
|
"param_grid = {'C':[0.01,0.1,1],'kernel':['linear','rbf']}\n",
|
||
|
"grid = GridSearchCV(svm,param_grid)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 22,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"GridSearchCV(estimator=SVC(),\n",
|
||
|
" param_grid={'C': [0.01, 0.1, 1, 10], 'kernel': ['linear', 'rbf']})"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 22,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Note again we didn't split Train|Test\n",
|
||
|
"grid.fit(X,y)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 23,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"1.0"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 23,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# 100% accuracy (as expected)\n",
|
||
|
"grid.best_score_"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 24,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{'C': 0.01, 'kernel': 'linear'}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 24,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"grid.best_params_"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"This is more to review the grid search process, recall in a real situation such as your exercise, you will perform a train|test split and get final evaluation metrics."
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"anaconda-cloud": {},
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"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.5"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 1
|
||
|
}
|