UniTO/anno3/apprendimento_automatico/esercizi/4/svm.ipynb

505 lines
256 KiB
Text
Raw Normal View History

2020-06-23 21:53:50 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Support Vector Machines"
]
},
{
"cell_type": "code",
2020-07-02 11:58:47 +02:00
"execution_count": 1,
2020-06-23 21:53:50 +02:00
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.svm import SVC"
]
},
{
"cell_type": "code",
2020-07-02 11:58:47 +02:00
"execution_count": 2,
2020-06-23 21:53:50 +02:00
"metadata": {},
"outputs": [],
"source": [
"X = np.array([[ 0.46613554, 0.92048757],\n",
" [-0.92129195, 0.06723639],\n",
" [-0.15836636, 0.00430243],\n",
" [-0.24055905, -0.87032292],\n",
" [ 0.06245105, -0.53698416],\n",
" [-0.2265037 , -0.43835751],\n",
" [-0.00480479, -0.17372081],\n",
" [-0.1525277 , -0.34399658],\n",
" [-0.27360329, 0.35339202],\n",
" [-0.77464508, -0.48715511],\n",
" [-0.58724291, 0.74419972],\n",
" [-0.97596949, -0.72172963],\n",
" [ 0.42376225, -0.72655597],\n",
" [ 0.96383922, -0.23371331],\n",
" [ 0.16264643, -0.46949742],\n",
" [-0.74294705, -0.42576417],\n",
" [ 0.05089437, -0.20522071],\n",
" [-0.19442744, 0.09617478],\n",
" [-0.97102743, 0.79663992],\n",
" [ 0.0596995 , -0.70129219],\n",
" [-0.83934851, -0.95616033],\n",
" [-0.38249705, 0.4973605 ],\n",
" [ 0.3474666 , 0.70664397],\n",
" [ 0.35871444, 0.88679345],\n",
" [-0.05914582, 0.23124686],\n",
" [-0.52156643, 0.32986941],\n",
" [-0.53579646, 0.67530208],\n",
" [ 0.13683914, -0.96158184],\n",
" [ 0.65904541, -0.12015303],\n",
" [-0.69078363, 0.5615536 ],\n",
" [ 0.47738323, -0.70919275],\n",
" [ 0.93069669, 0.44019132],\n",
" [ 0.19750088, -0.68869404],\n",
" [-0.75048675, -0.18170522],\n",
" [-0.45288395, -0.25894991],\n",
" [-0.74644547, 0.87781953],\n",
" [ 0.14620452, 0.56864508],\n",
" [ 0.25719272, -0.58405476],\n",
" [ 0.87149524, 0.01384224],\n",
" [-0.71473576, 0.31568314],\n",
" [-0.252637 , -0.67418371],\n",
" [ 0.24718308, 0.95191416],\n",
" [-0.38149953, -0.64066291],\n",
" [-0.23112698, 0.04678807],\n",
" [ 0.72631766, 0.7390158 ],\n",
" [-0.91748062, -0.15131021],\n",
" [ 0.74957917, 0.66966866],\n",
" [ 0.76771849, 0.06662777],\n",
" [-0.04233756, -0.91320835],\n",
" [ 0.63840333, 0.06277738],\n",
" [-0.78887281, -0.90311183],\n",
" [-0.73099834, -0.69587363],\n",
" [-0.50947652, -0.99144951],\n",
" [ 0.14294609, 0.5474932 ],\n",
" [ 0.4367906 , 0.31953258],\n",
" [-0.13970851, 0.81817884],\n",
" [ 0.6440873 , 0.79118775],\n",
" [ 0.41714043, -0.66672029],\n",
" [ 0.59283022, -0.71836746],\n",
" [ 0.55379696, 0.98846202],\n",
" [-0.91819517, 0.34203895],\n",
" [ 0.02020188, 0.83696694],\n",
" [ 0.6182918 , 0.04254014],\n",
" [-0.09354765, -0.30050483],\n",
" [-0.08489545, 0.06431463],\n",
" [-0.11886358, -0.68738895],\n",
" [ 0.44428375, 0.18273761],\n",
" [ 0.26486362, -0.98398013],\n",
" [ 0.13222452, 0.91495035],\n",
" [-0.11101656, 0.00541343],\n",
" [-0.07696178, -0.92720555],\n",
" [ 0.22602214, 0.56040092],\n",
" [ 0.74227542, 0.32930104],\n",
" [ 0.43524657, 0.35332933],\n",
" [-0.89277607, -0.59996171],\n",
" [-0.94836212, 0.78777302],\n",
" [ 0.1783319 , -0.2142071 ],\n",
" [-0.07832238, -0.25046584],\n",
" [ 0.17611799, -0.96927832],\n",
" [-0.95938454, -0.26504646],\n",
" [ 0.58666766, -0.94620881],\n",
" [-0.77336565, 0.46735057],\n",
" [-0.94414054, 0.39044333],\n",
" [ 0.61524645, 0.15907662],\n",
" [-0.09855302, 0.9816656 ],\n",
" [ 0.53937097, 0.34487634]])"
]
},
{
"cell_type": "code",
2020-07-02 11:58:47 +02:00
"execution_count": 3,
2020-06-23 21:53:50 +02:00
"metadata": {},
"outputs": [],
"source": [
"y = [\"red\" if x + y > 0.3 else \"green\" for [x,y] in X]\n"
]
},
{
"cell_type": "code",
2020-07-02 11:58:47 +02:00
"execution_count": 4,
2020-06-23 21:53:50 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2020-07-02 11:58:47 +02:00
"<matplotlib.collections.PathCollection at 0x7f75285b5b90>"
2020-06-23 21:53:50 +02:00
]
},
2020-07-02 11:58:47 +02:00
"execution_count": 4,
2020-06-23 21:53:50 +02:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2020-07-02 11:58:47 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD4CAYAAADsKpHdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOzdd3xUVdrA8d+ZkmQmCSUQekITpColICBKlWIBFVAsi7AIFsC2rq69sO+C2LtSpKmAYAGUoiAIKiVBeu+SAEmAAEkmybTz/nGTkDATCMnM3Exyvn7yIXPuzL1PDMwz95TnCCkliqIoimLQOwBFURSlbFAJQVEURQFUQlAURVFyqYSgKIqiACohKIqiKLlMegdQEtWrV5cNGjTQOwxFUZSgsmnTplNSyuiijgdlQmjQoAEJCQl6h6EoihJUhBBHL3VcdRkpiqIogEoIiqIoSi6VEBRFURRAJQRFURQll08SghDiCyFEihBiRxHHhRDiAyHEASHENiFEuwLHHhBC7M/9esAX8SiKUs643bBpE6xfDw6H3tGUW766Q5gB9LvE8f5Ak9yv0cCnAEKIKO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rThw/33KO5PMjI8D4Q7XZ73wuiHPJFQkgCYgo8rpfb5s1QYE7BBillUu6fh4DVQFsfxFShDbh6AFazFYMo/Os1G80MbVXMT0uKAloBwBEjtIV7WVnanP5587S28qZHD63b6GLh4TB4cODj0YEvEkI80EQI0VAIEYL2pr/o4icJIZoBVYF1BdqqCiFCc7+vDlxP0WMPSjG
2020-06-23 21:53:50 +02:00
"text/plain": [
2020-07-02 11:58:47 +02:00
"<Figure size 432x288 with 1 Axes>"
2020-06-23 21:53:50 +02:00
]
},
2020-07-02 11:58:47 +02:00
"metadata": {
"needs_background": "light"
},
2020-06-23 21:53:50 +02:00
"output_type": "display_data"
}
],
"source": [
"plt.scatter(X[:,0], X[:,1], c=y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"\n",
"- import the support vector machine classifier from scikit-learn (the SVC class) and train a classifier for the examples above using a linear kernel;\n",
"- read the documentation to find out how to obtain the support vectors and the associated (dual) weights; use this information to analyze the learnt model: \n",
" - how many support vectors have been learnt? \n",
" - are them in the position you would have expected [[1](#note1)]? \n",
" - is there any margin error?\n",
" - is there any classification error (check it using the classifier predictions)?\n",
"- learn a new SVC model using custom C values:\n",
" - how the answers to the questions above change when you use a very high C value (e.g., 1000)?\n",
" - how the answers to the questions above change when you use a very low C value (e.g., 0.3)?\n",
"- learn a new SVC model using a rbf kernel:\n",
" - is the new kernel able to capture the linear model?\n",
" - are you surprised by the above answer? Regarless to whether you are surprised or not: why?\n",
" \n",
"<a name=\"note1\">[1]</a> If you make two plots one after the other (in the same cell), the plots will be merged into a single one. You may want to use this feature to plot the support vectors on top of the scatter plot for the dataset."
]
},
2020-07-02 11:58:47 +02:00
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def ex(clf, X, y):\n",
" sv = clf.support_vectors_\n",
" weights = clf.dual_coef_\n",
"\n",
" # plot support vector\n",
" p = plt.scatter(X[:,0], X[:,1], c=y)\n",
"\n",
" ax = plt.gca()\n",
" xlim = ax.get_xlim()\n",
" ylim = ax.get_ylim()\n",
"\n",
" xx = np.linspace(xlim[0], xlim[1], 30)\n",
" yy = np.linspace(ylim[0], ylim[1], 30)\n",
" YY, XX = np.meshgrid(yy, xx)\n",
" xy = np.vstack([XX.ravel(), YY.ravel()]).T\n",
" Z = clf.decision_function(xy).reshape(XX.shape)\n",
"\n",
" ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,\n",
" linestyles=['--', '-', '--'])\n",
"\n",
" ax.scatter(sv[:, 0], sv[:, 1], s=100,\n",
" linewidth=1, facecolors='none', edgecolors='k')\n",
" plt.show()\n",
"\n",
" print(f\"Support vectors: ⊕:{clf.n_support_[0]} ⊖:{clf.n_support_[1]}\")\n",
" print(f\"Numero totale di support vectors:{len(sv)}\")\n",
"\n",
" ev = clf.decision_function(sv)\n",
" merr = []\n",
" for i, ep in enumerate(ev):\n",
" ep = round(-1-ep if ep < 0 else 1-ep, 3)\n",
" if ep: merr.append(f\"\\tMargin error ⊖: {ep}\\n\")\n",
" print(f\"Margin error totali: {len(merr)}\")\n",
" print(''.join(merr))\n",
"\n",
" y_predicted = clf.predict(X)\n",
" classification_error = len(list(filter(lambda ic: ic[1] != y[ic[0]], enumerate(y_predicted))))\n",
" if not classification_error:\n",
" print(\"Nessun errore di classificazione\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD4CAYAAADsKpHdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydZ1hU19qG7z0zdEEQsCtYYsGu2NCIxN7FoAJ2BZNoYtpJ8ZTULycxyUnXRAW72Ct2MfZCxIq9oYgNEZAmMMys78dGggKKM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uD+JxESoV09+8D78jmo04OYmp2wwpsNGCKhUCQpbae/uDjExxbvO4cOyU0ink91GFSrIw0V//llgHYhC6aAMGZUx1p9fT7Yuu8B2ndCx6OQirKys6Nq1K1euXCE0NJSkx3O4mCEajYZBgwbRvXt3oqOjmT9/PmlpaaaWVWIeOquGDx+OSqUiIyODrKwsVq9ejbe3N25ubnh5eVG1alXGjh37bMNmlSrJbidPT9mBY2EB3bvD3r3Gt1tmZ0NKSuH7bt0q/nXatoVr1+Sexj//CcuXy/eoBIMygxIQjMydtDuFBoTMnExupMqLtFu3bs2oUaNITU0lJCSkXGTalCSJzp07M2zYMG7fvk1ISAjx8fEm0aLVaomKimLv3r0lTiWiUqmwsrJCCMGyZcsYMGAAH3/8MW+++Sb3YmOJO3iQKxcu0LhxY3x8fNi+fXvxL96kCfz1F9y7J7+db94M1aqVSO9zYWkJtWoVvq9pgVyWT8bJCSZNgq++khe
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Support vectors: ⊕:8 ⊖:9\n",
"Numero totale di support vectors:17\n",
"Margin error totali: 15\n",
"\tMargin error ⊖: -0.085\n",
"\tMargin error ⊖: -0.028\n",
"\tMargin error ⊖: -0.083\n",
"\tMargin error ⊖: -0.441\n",
"\tMargin error ⊖: -0.027\n",
"\tMargin error ⊖: -0.258\n",
"\tMargin error ⊖: -0.001\n",
"\tMargin error ⊖: -0.156\n",
"\tMargin error ⊖: 0.238\n",
"\tMargin error ⊖: 0.222\n",
"\tMargin error ⊖: 0.277\n",
"\tMargin error ⊖: 0.507\n",
"\tMargin error ⊖: 0.2\n",
"\tMargin error ⊖: 0.007\n",
"\tMargin error ⊖: 0.029\n",
"\n",
"Nessun errore di classificazione\n",
"\n"
]
}
],
"source": [
"clf = SVC(kernel=\"linear\")\n",
"clf.fit(X,y)\n",
"ex(clf, X, y)\n",
"print()"
]
},
{
"cell_type":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LmBlbDCxdaFn4J/ok/jPpV+hpOTEyEhIYDiXDZu3EiXLl1UdRAeHh7MmDGDrVu3Eh8fT3JyMsHBwdSoUUM1G0AR6ktOTmbnzp1cvXqVsWPHVv8wUuvWSjjl4aY8nQ6aNn3y59WsCRMnPr09n3yiVCbds6ewED74ANzcYMaMx9/fvDnUqQNXrjx43MFBSehrPBdoISOVKSwupOEXDbldcPuB4042Tlz8zUU8nZ88J3D37l3mz59PQUEBgYGBdOnSRfUy1bNnz7J69WqklAQFBdG2bVtV15dScuTIETZu3Ii9vT3BwcFUpzGrZcjMhGbNICvr17dwnQ4aN1YkG9RM5ksJNWpAeSXH3t5w+XLlnpOQoFQKGY2KY3F2VsJFO3ZU3AeiYVa0kNFzxtqkteiN+jLHjdLIsuPLnuqZ7u7uhIWF4e3tzfr164mNjaWoqOhZTX0iWrduTVhYGLVr12bFihVs3LhR9RBS586dmT17No6OjlU2N9ps1KihVDv5+ioVODY2MGgQ7N2rfrmlXg/Z2eWfS02t/HO6doWrV5Wdxp/+BCtWKD+j5gyeG7SQkcrczL1ZrkMoLC4kJSflqZ/r5OTEpEmT2LdvHzt27CA3N5dp06Y9g6VPjru7O9OnT2fbtm388ssvJCcnExISomoIydPTk7CwsFKHcOTIEZo3b46Li0vpNQaDgWPHjlFQUECTJk1oaCmBtLw85U2/Xr0nS8Deo107OHgQcnIUJ+DoaH4bK4OtLXh5KR/mD9O+jJblo/HwUNRkNZ5LqvlrVPWjd6PeWFuVfcNztnWmv3f/Z3q2EILevXszbdo0/P39AaVUVc2woLW1NUOGDOG
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Support vectors: ⊕:1 ⊖:2\n",
"Numero totale di support vectors:3\n",
"Margin error totali: 1\n",
"\tMargin error ⊖: -0.001\n",
"\n",
"Nessun errore di classificazione\n"
]
}
],
"source": [
"svc = SVC(kernel = 'linear', C = 1000)\n",
"ex(svc.fit(X, y), X, y)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD4CAYAAADsKpHdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydd1hU19aH3zP0XgS7ArbYFcWeKHaxVwQssYFGjTHJl2vKjUnMNYnGFGOagr2AvSvYK6hgxYrGgmBBEKTDMLO/PwaNCijqFAbmzTNP5LS9zjDMOnuvtX5LEkJgwIABAwYMyHRtgAEDBgwYKBkYHIIBAwYMGAAMDsGAAQMGDORjcAgGDBgwYAAwOAQDBgwYMJCPsa4NeB2cnJyEq6vra5+fk5ODUqnEwsJCfUaVQvLy8khMTMTW1hZLS0tdm1OiUSgUJCYmYm1tjZWVla7NMWCgUE6ePJkohHAuar9eOgRXV1eioqJe+/zVq1dz6dIl2rVrR+fOnZHJDBOlwsjOzmbdunVcu3aNVq1a0b17d8N7VQS5ubls2LCBy5cv4+HhgZeXF0ZGRro2y4CBZ5Ak6daL9pfJv+7Bgwfj4eHB0aNHWb16NTk5Obo2qURibm6On58frVu35vjx46xatYrs7Gxdm1UiMTU1ZejQobz99ttERUURHByMocbHgL4h6eOH1sPDQ7zJDOExJ06cIDQ0FCcnJ8aMGYO5ubkarCudnDx5ku3bt+Po6Iivry/lypXTtUkllrNnz6JUKnF3d9e1KQYMPIMkSSeFEB5F7i/LDgHgn3/+ISYmhh49eiBJklquWVq5efMma9asQQiBt7c3bm5uujapxHPx4kVMTEyoXbu2rk0xYMDgEF6FxMRE4uLiaNq0qdqvXVpITk5m1apVJCUl4eXlRYsWLXRtktpITExk5cqV3LhxA0tLS/r06UPr1q1f+0FBCMGiRYuIi4uje/futGrVyvDQYUCnv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KoNcrmcTZs2cWHlSpqGhtI7J+dZjXorK9izB1q3Lv5FlyxRBZaNjVWORKGAVatUS2SvSm4uPHwITk6q6xWXohwTQKNGcO7cq9ti4JV5mUPQy34IJQV3d3cSExOJiIggMTGRIUOGvFAxNSkp6UmAOjw8HIW9ArlSjl2uHfN+m0fr1q05ePBgqRONeywNvW/fPg4fPkxSUhLe3t4lrsbDyMiI7t2769QGExMTBg8eTPkVK9ifk0MS4AM8CakqlXDmzKs5hFGjVFlGYWGqdXcvL3jVYkulEmbMgJ9+UqWBmpnBV1/B1KnFm60oFEUfpwfZaGUFg0N4A2QyGd27d8fZ2Znt27cTFBT0Qmno9957j+7duzN62mi8VntxJ+0OMkmGjakNqyetpnLlygwbNozw8HAt34nmkSSJzp074+zszJYtWwgMDMTPzw9t1JToG5Ik0aFTJ5x27eJQdjbPfI0aG0ONGq9+0XLlwM/v9Y364QdVZlJmpurn7Gz473/Bzg7GjHn5+bVqQfnycPPms9stLOBdnUqYGXgKw5KRmrh16xYbNmzA29ubKlUKFlvHxcXRuHFjrvxzhXqB9UjKSnpmv5WJFTGTYmjTuA3r16/Hw6PIWZ3eExcXR0hICHK5nMGDB5daaejk5GTS0tJwcnJ69dnQw4dQsybKlBRkgAKINTLCzdVVJdmgzSU3IcDREVJSCu5zdYUbN4p3nchIVaaQQqFyLNbWquWiffuKrgMxoFZKinRFqcfFxYX333//iTN4XgwtLCyMnj17sv/OfnIVuQXOVwgFwReC8fX1Zdu2bVqxWVdUrVoVf39/HBwcWLVqFREREaWqu9iGDRvo0KEDLi4utG3blooVKzJq1KhXq3Z3dIQjR5C1aAEmJoQbGbHU1ZXDc+YgtN3
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Support vectors: ⊕:17 ⊖:16\n",
"Numero totale di support vectors:33\n",
"Margin error totali: 30\n",
"\tMargin error ⊖: -0.281\n",
"\tMargin error ⊖: -0.251\n",
"\tMargin error ⊖: -0.011\n",
"\tMargin error ⊖: -0.066\n",
"\tMargin error ⊖: -0.035\n",
"\tMargin error ⊖: -0.284\n",
"\tMargin error ⊖: -0.534\n",
"\tMargin error ⊖: -0.248\n",
"\tMargin error ⊖: -0.151\n",
"\tMargin error ⊖: -0.132\n",
"\tMargin error ⊖: -0.093\n",
"\tMargin error ⊖: -0.378\n",
"\tMargin error ⊖: -0.213\n",
"\tMargin error ⊖: -0.064\n",
"\tMargin error ⊖: -0.318\n",
"\tMargin error ⊖: 0.071\n",
"\tMargin error ⊖: 0.526\n",
"\tMargin error ⊖: 0.496\n",
"\tMargin error ⊖: 0.277\n",
"\tMargin error ⊖: 0.537\n",
"\tMargin error ⊖: 0.287\n",
"\tMargin error ⊖: 0.695\n",
"\tMargin error ⊖: 0.331\n",
"\tMargin error ⊖: 0.351\n",
"\tMargin error ⊖: 0.491\n",
"\tMargin error ⊖: 0.343\n",
"\tMargin error ⊖: 0.237\n",
"\tMargin error ⊖: 0.171\n",
"\tMargin error ⊖: 0.347\n",
"\tMargin error ⊖: 0.032\n",
"\n",
"Nessun errore di classificazione\n"
]
}
],
"source": [
"svc = SVC(kernel = 'linear', C = 0.3)\n",
"ex(svc.fit(X, y), X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Notiamo che maggiore l'indice di regolarizzazione, minore il numero di errori*\n",
"\n",
"Il tempo necessario al training non subisce in aumento sensibile"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD4CAYAAADsKpHdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydd1xUV/r/32dm6CCKgIodrNgVsYst2L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CCdlctAgGDHh2XXo9JCeDu7vSX1EpzDABNGkCx48/uxYrISsri+XLl/P777/j7+9Pz549rXY50Vr2IbyQODk54efnx5gxY3jnnXfo27cv9wv37Ny5k2nTprFt2zbu75u4c+cOHTt25M6dO+zZs4efd/zM4s2LiT0ei7OzM23btuXSpUsWfEclS7ly5QgNDSUrK4vFixej1+stLekRtFotPXv2ZNKkSYwePfqJI38hBB07diQhIaFkIk72789vDECJejp27Nn6GjsW4uNh+nRlHf7GjWc3BiaTsvRUsSLUrq0YhK+/Vkb+RcFoLHxWU8Zj+x0cHBg5ciTt27fn7NmzZTqSsUwWyLFGnJ2dad269YPX7u7uODk5sX37drZt24anpyfLly8nMDCQce+Po/fS3ly/dx2N0OBi68LSN5bi5eXFyJEj2bNnjwXfSclSpUoVgoODWbJkCT///DPDhg0rs/6X5s2bs3v3brZs2UK9evXMm/SuQQNlOeV+dNB9dDrw9n72/ipWhNDQ4uuZMkWJTLqvJzsb/vY3cHWFceOefn2dOuDpCZcvP9ru4KBEO5VxNBoNgYGBdOr
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Support vectors: ⊕:9 ⊖:7\n",
"Numero totale di support vectors:16\n",
"Margin error totali: 10\n",
"\tMargin error ⊖: -0.015\n",
"\tMargin error ⊖: -0.429\n",
"\tMargin error ⊖: -0.184\n",
"\tMargin error ⊖: -0.197\n",
"\tMargin error ⊖: 0.122\n",
"\tMargin error ⊖: 0.216\n",
"\tMargin error ⊖: 0.001\n",
"\tMargin error ⊖: 0.052\n",
"\tMargin error ⊖: 0.449\n",
"\tMargin error ⊖: 0.132\n",
"\n",
"Nessun errore di classificazione\n"
]
}
],
"source": [
"rbf = SVC(kernel=\"rbf\")\n",
"rbf.fit(X,y)\n",
"ex(rbf, X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*come atteso, il rilsultato è lo stesso nonostante le classi vengano mappate su uno spazio ad infinite dimensioni.*\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD4CAYAAADsKpHdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydd1xUZ77/38/M0EFQLIii2Bv2rlhRVOwa7F1jjCV7N7v722Tvbu4mezf9pqzR2HvvUcGCvURRrKBEJRaKCkQU6WXm+f1xgKiAosCcQc87L14yp34GwnzP861CSomGhoaGhoZObQEaGhoaGpaBZhA0NDQ0NADNIGhoaGho5KAZBA0NDQ0NQDMIGhoaGho5GNQW8CpUrFhRenp6lvp9pJTEx8dTsWJFdLrX13ampKSQnp6Oq6ur2lIKxGQykZiYiJ2dHba2tmrL0XhDSEtL4/Hjx7i6umIwlMmPynycO3fuNyllpcL2l8l36enpSUhISKnfx2Qycf/+fdzd3ZFScvLkSZo3b46Tk1Op39ucXLx4kR07duDn50e7du3UlpMPk8nEf/7zH5ydnZk8ebLacjTeEO7du8fq1avR6/VMmDCBSpUK/RwtMwgh7jxv/+v72FsC6HQ63N3dAYiPj+fw4cPMnTuX48ePk5WVpbK6kqN58+bUrVuXAwcO8OjRI7Xl5EOn09GxY0fKlStHdna22nI03hCqVq3KpEmTkFKyfPly7t+/r7akUkeUxcK0Nm3aSHOsEJ4lISGBoKAgwsPDcXZ2pnfv3jRp0gQhhNm1lDSJiYnMmzeP6tWrM378+NfiPWlolAQPHjxg1apVZGRkMGvWrDLtIRBCnJNStilsv7ZCeAkqVKjAyJEjmTRpEnZ2duzbt4+MjAy1ZZUIzs7O+Pr6cvPmTS5cuKC2nEKJjY0lJSVFbRkabxCurq5MnjyZ7t27l2ljUBQ0g/AKeHp6Mn36d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IKtGiRQt83vIhWZfMunXr2L17d5lox+Dp6cno0aOJj49n9erVpKenqy0JUFJRc9NQb926VfZ7SwUHK4HgZzGZ4GULoSZNUoq4Fi6ExYvh3j0lXvIymEyK68nVFWrVUuIr335btBgAKHUDha1qXoN0zcK4cOECa9eu5dixY2pLKRJlckBOWScsLoyhG4dyN+kuwiSwuWfDkENDaNq0KTVr1lRb3gupU6cOI0eOZOPGjaxdu5Zx48ZhYyGpmdnZ2Wzfvh2dTseUKVMsKtbxUjRsqDzJp6Y+vd1gUJ76XxZXVxgz5tX1fP65kpmUqyc9Hf7+d3B2hilTXnx+3bpQuTI8W8FvZ6cE9F9TWrZsSWRkJIcOHSIrK4uePXtadNxQiyGYmfTsdKp/U50HaU8/wdpn2XPzg5tUcaxCZGQkHh4eFv0/DkB4eDibN2/Gw8ODsWPHYq1mAPYJ7t27x4oVK3BycmLSpEk4OjqqLenlSUiAOnUgMfH3p3CDAWrWhGvXzJthIyVUqAAFVax7eiruoKJw9qySKWQ0KobF0RGaNoVDhwqvA3kNkFKye/duzp07R8eOHfH19VXtb1uLIVgYO6/tJNOYv+rXZGdizeU1xMbGsnz5clavXm3xff8bNWrEsGHDiIyMZMOGDRbj8qpatSpjxowhMTGRVatWWfzPsUAqVFCyndq0UTJwrKygVy84ftz86ZaZmVBYO/R794p+nbZt4c4dZaXxt7/Bpk3Ke3yNjQEomYQDBgygffv2hISEWLQ7UzMIZiY2ObZAg5CenU5MUgyVK1dmwIABREVFsXjxYosvZPPy8mLIkCHcunWLTZs2WUyNRc2aNfMC4M+uJrOysggJCeH48eNEl2aDtJQUJZD6qj+TJk3gzBl48EB5Ot+zB6p
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Support vectors: ⊕:5 ⊖:5\n",
"Numero totale di support vectors:10\n",
"Margin error totali: 1\n",
"\tMargin error ⊖: 0.001\n",
"\n",
"Nessun errore di classificazione\n"
]
}
],
"source": [
"rbf = SVC(kernel=\"rbf\", C=100)\n",
"rbf.fit(X,y)\n",
"ex(rbf, X, y)"
]
},
2020-06-23 21:53:50 +02:00
{
"cell_type": "code",
"execution_count": null,
2020-07-02 11:58:47 +02:00
"metadata": {},
2020-06-23 21:53:50 +02:00
"outputs": [],
"source": []
}
],
"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",
2020-07-02 11:58:47 +02:00
"version": "3.7.7"
2020-06-23 21:53:50 +02:00
}
},
"nbformat": 4,
"nbformat_minor": 1
}