UniTO/anno3/altro_muovi/marco/svm.ipynb

618 lines
145 KiB
Text
Raw Normal View History

2020-06-17 20:01:41 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Support Vector Machines"
]
},
{
"cell_type": "code",
"execution_count": 1,
"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",
"execution_count": 2,
"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",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"y = [\"red\" if x + y > 0.3 else \"green\" for [x,y] in X]\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x1f2117ed748>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD4CAYAAADhNOGaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3xT1fvA8c/Japu2UKDsskfZiFREUEHZoiwVwYUCgorrh4j6daCIe4AKDkRUREVUUEQRGQUVUECEAkLZe0NLR9pmnd8fN9SWpNCR9LbJeb9efdGc3Nz7FGie3LMeIaVEURRFCV0GvQNQFEVR9KUSgaIoSohTiUBRFCXEqUSgKIoS4lQiUBRFCXEmvQMojtjYWFm/fn29w1AURSlX/v7771NSyqrnt5fLRFC/fn3Wr1+vdxiKoijlihBiv6921TWkKIoS4lQiUBRFCXEqESiKooQ4lQgURVFCnEoEiqIoIc4viUAIMVMIcUIIsaWA54UQ4h0hxC4hRJIQ4tI8zw0TQuz0fA3zRzyKoihK4fnrjuBToPcFnu8DNPF8jQLeBxBCVAYmAJcDHYAJQohKfopJUYrObof16yE5We9IFAApYfp0iI+H2Fi48UbYuVPvqIKOXxKBlPI34MwFDukPzJKaP4EYIURNoBewREp5RkqZAizhwglFUQLnu++genW49lq49FJo1Qr27NE7qtD2xBPwf/8HO3bA6dPw/fdw2WVw4IDekQWV0hojqA0czPP4kKetoHYvQohRQoj1Qoj1J0+eDFigSoj691+4805ITYX0dLDZYNs26NYN3G69owtNqanwzjvav8U5brf2+NVX9YsrCJVWI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3wd4abwCx7ncDloFtssUGGUqjNZZxj36zgavN2AltNaMm3dNFxu9YtAt27aHPhjx2DxYujQQe+IFH+Kj9emBZ8vIgKGDbvwa+12+PVX7cNCenpg4iuDSjx9VAhhBHYAPdCK0a8Dhkop/y3g+AeBdlLK4Z7HGVLKIo3OFnf6aI4zh2cSn2H639PJsGcgELilGzfaJwir2cr4TuOZ0HVCkc9d1mTaM2n9fmsOpx/OvROymq0MbDaQ2YNm6xydogTYuUV85+o4REXBJZfA0qX/1WY436pVcMMN/901OBzamMIdd5Re3AEWsAplQogrgOeklL08j58EkFK+XMDxq4EJUsolnsellgjOtzdlLxNWTGDpnqVUi6zG+M7jGdpqaFCsL/hw/Yc8+uujZDoy87WHm8JJujdJbXetBL+UFK3m8ZEj2tqOHj0K7qqz2aBmTe/1HxERWv2G+PjAx1sKCkoE/pg+Whs4mOfxIeDyAoKoBzQAludpDhdCrAecwCtSyu8LeO0oYBRA3bp1/RA2NKjUgFkDZ/nlXGVN4r5EryQAYDKYWHt4rUoEStGcOqXNwKlbt/yMe1SqpNV5LoyFC32vM3A64bPPtEpvQcwf/6K+Pj4XdJsxBPhWSpm3o7quJ0PdCkwRQjTy9UIp5XQpZYKUMqFq1aolizgENKrUyOcsKYEgroIqBKIU0qlT2oB6XBy0aKFtK7F4sd5R+V96uu+BZIcDUlO924OMPxLBISDvpiNxQEHTboYAX+VtkFIe8fy5B1gBqO03/WBU+1GYDfkXzhiFkRpRNbiq3lU6RaWUO717w4oV2qKsrCytm2XQINi2Te/I/Kt7d9+zjSIjoV+/0o+nlPkjEawDmgghGgghLGhv9l6zf4QQ8UAlYE2etkpCiDDP97FAZ8DnILNSNPVi6vHTrT9Rp0IdrCYrYcYwOsZ1JHFYIgaR/5/d5rAVamqtEmI2boTt27VPxXnl5GiL5oJJvXrw6KP
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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."
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
" decision_function_shape='ovr', degree=3, gamma='auto_deprecated',\n",
" kernel='linear', max_iter=-1, probability=False, random_state=None,\n",
" shrinking=True, tol=0.001, verbose=False)"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Creazione SVC\n",
"svc = SVC(kernel = 'linear')\n",
"svc.fit(X, y)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of support vectors learnt: 17\n"
]
},
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x1f213591408>"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD4CAYAAADhNOGaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AA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W5AW4x+PTBESunxb1EI8TjQUUo5wvU5V0pZqtabsjYWSymZsWkGH/39EemmdHIsOdiddswOMwJBqCGUyTdPZnjHSrCe6yWcMZ0hfsTLmL7/CGwXGi11xnxmzwjgqluK4g/790Pv3toU106n9jVwoLYOQ3GD4JKTYeRI7XuHQ5vk76mntDUdqgifrVAmhOgBvC6lvMn1+WUAKeU7xez/J/CalHKZ67PfEkFRWeYspqyfws97fya2RixPdXuKHo17lPu8FcHENRN5+bYhyGz3bowNGlk4kaq60SlVnN0Oy5fDyZPaEp8tWhS/b3q6NmVG0UV/wsLgt9/gyit9G6uf+HIcQSPgaIHPqYDHVS+EEPFAE2BFgeIQIUQKYAcmSCk9Tq8ohEgEEgHi4txvbmVRK6QWr1zzCq9c84pXzleRbDyxEZn9vMdtJ48b/RyNUqk5HNpMoVlZ2syh/lrnuLyCgrR1EEpi8WJt/6Ly8+Hrr6tMIiiON9oIPE2rWNxrxmBgvpSy4Hj+OFeGug/4SAhxmacDpZRJUsouUsoudYtbtEM5r1NMJ0TNVI/bGjT0Yg8ZpWrbsQPi47XpoocNg0aNqmYXzOJqRoQoflsV4o1EkAoU7HIRCxwvZt/BwNcFC6SUx11/HgBWAh29EFO193Cnhwnr9zYYCneV1BnNvDdRVQspJeB0wk03aQ2oOTlafbvZDK++Cn/8Eej
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"print(\"Number of support vectors learnt:\", sum(svc.n_support_))\n",
"plt.scatter(X[:,0], X[:,1], c=y)\n",
"plt.scatter(svc.support_vectors_[:,0], svc.support_vectors_[:,1], c='b')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sono nelle posizioni che mi aspettavo ma sono un numero più elevato di quello atteso"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C'è margin error perchè i support vector sono a distanze diverse dal margine geometrico"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Classification errors: 0\n"
]
}
],
"source": [
"predicted = svc.predict(X)\n",
"errors = sum([y[i] != predicted[i] for i in range(len(y))])\n",
"print(\"Classification errors:\", errors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# C = 1000"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SVC(C=1000, cache_size=200, class_weight=None, coef0=0.0,\n",
" decision_function_shape='ovr', degree=3, gamma='auto_deprecated',\n",
" kernel='linear', max_iter=-1, probability=False, random_state=None,\n",
" shrinking=True, tol=0.001, verbose=False)"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"svc = SVC(kernel = 'linear', C = 1000)\n",
"svc.fit(X, y)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of support vectors learnt: 3\n"
]
},
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x1f213384908>"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD4CAYAAADhNOGaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3gU1dfA8e/dlmSTQIDQQ+iEXiQiRUWlCKKAqAiiIohg1xcR209Q7AVBBQsiKooiKiigiHSlSJESRAi9Q2gJJNkk2+77x4SQsBtI2c0ku/fzPDwkd2ZnTih7dm47QkqJoiiKErwMegegKIqi6EslAkVRlCCnEoGiKEqQU4lAURQlyKlEoCiKEuRMegdQFNHR0bJOnTp6h6EoilKm/PPPP6eklJUvbi+TiaBOnTps2LBB7zAURVH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Zw6pc3AiY0tO+MeFSpodZ4LYv587+sMnE746iut0lsA88XfqLePz/k9ZgwAfpRS5u6ojs3OUHcBE4UQ9b29UEo5RUoZL6WMr1y5cvEiDgL1K9T3OktKIIgppwqBKAV06hR0764Vj2naVNtWYuFCvaPyvdRU7wPJDgekpHi2BxhfJILDQO5NR2KA/KbdDAC+y90gpTya/fteYDmgtt/0geFth2M25F04YxRGqkVU45ra1+gUlVLm9OgBy5dri7IyMrRuln79YPt2vSPzra5dvc82Cg+H3r1LPp4S5otEsB5oKISoK4SwoL3Ze8z+EULEARWANbnaKgghQrK/jgY6AV4HmZXCqR1Vm1/v+pVa5WphNVkJMYbQPqY9ywYvwyDy/rX
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"print(\"Number of support vectors learnt:\", sum(svc.n_support_))\n",
"plt.scatter(X[:,0], X[:,1], c=y)\n",
"plt.scatter(svc.support_vectors_[:,0], svc.support_vectors_[:,1], c='b')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sono i support vector che mi aspettavo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Non c'è margin error"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Classification errors: 0\n"
]
}
],
"source": [
"predicted = svc.predict(X)\n",
"errors = sum([y[i] != predicted[i] for i in range(len(y))])\n",
"print(\"Classification errors:\", errors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# C = 0.3"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SVC(C=0.3, cache_size=200, class_weight=None, coef0=0.0,\n",
" decision_function_shape='ovr', degree=3, gamma='auto_deprecated',\n",
" kernel='linear', max_iter=-1, probability=False, random_state=None,\n",
" shrinking=True, tol=0.001, verbose=False)"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"svc = SVC(kernel = 'linear', C = 0.3)\n",
"svc.fit(X, y)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of support vectors learnt: 33\n"
]
},
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x1f213543a88>"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD4CAYAAADhNOGaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeVxU1fvA8c+ZDRgUkcUFEbC0RW1RsX3PUtv9ZmWh5VeLbF9+fcuy1cKtXCq0ct/IpTK1rMw1bbFccs1MU0EUEWVngNnO748ZFZxBEWaB4bxfr3kJ596591HhPnPPOfc8QkqJoiiK0nBp/B2AoiiK4l8qESiKojRwKhEoiqI0cCoRKIqiNHAqESiKojRwOn8HUBNRUVEyISHB32EoiqLUKxs3bjwqpYw+tb1eJoKEhAQ2bNjg7zAURVHqFSFEurt21TWkKIrSwKlEoCiK0sCpRKAoitLAqUSgKIrSwKlEoCiK0sCpRKAoitLAeSQRCCGmCiGOCCG2V7FdCCE+EkLsEUJsFUJ0rrDtESHEbufrEU/EoyiKolSfp+4IpgM9TrO9J9DO+UoGPgEQQkQAbwGXA5cBbwkhmnooJkU5O1LChg2wZg2Ulfk7GiU/H558EiIiHK9BgyAvz99RBSSPJAIp5Rog9zS73A3MlA7rgHAhREugO7BMSpkrpcwDlnH6hKIo3rF9O2lRz5FwWTM0119DgvEIac/85u+oGi6bDa65BqZMcVz88/Jg2jS46iqwWv0dXcDx1RhBK+BAhe8znW1VtbsQQiQLITYIITbk5OR4LVClA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Ba6uhSOXYMCgsdL5PJUYj
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"print(\"Number of support vectors learnt:\", sum(svc.n_support_))\n",
"plt.scatter(X[:,0], X[:,1], c=y)\n",
"plt.scatter(svc.support_vectors_[:,0], svc.support_vectors_[:,1], c='b')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sono molti più support vector di quelli che mi aspettavo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C'è un margin error molto elevato"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Classification errors: 0\n"
]
}
],
"source": [
"predicted = svc.predict(X)\n",
"errors = sum([y[i] != predicted[i] for i in range(len(y))])\n",
"print(\"Classification errors:\", errors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Kernel = RBF"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\galat\\.conda\\envs\\aaut\\lib\\site-packages\\sklearn\\svm\\base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n",
" \"avoid this warning.\", FutureWarning)\n"
]
},
{
"data": {
"text/plain": [
"SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
" decision_function_shape='ovr', degree=3, gamma='auto_deprecated',\n",
" kernel='rbf', max_iter=-1, probability=False, random_state=None,\n",
" shrinking=True, tol=0.001, verbose=False)"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"svc = SVC(kernel = 'rbf')\n",
"svc.fit(X, y)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of support vectors learnt: 22\n"
]
},
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x1f21313c508>"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD4CAYAAADhNOGaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeVhUZfvA8e8zKwwKKOAOYrnkVplome1paqZle2FWWujbXr/esmx5yyyXTC2sNDU1yaUyl6zM3TbLJde0XEEUcWMfYLbn98dBBWdQhBkGhudzXVzCc86cc1s495xnu4WUEkVRFKXm0vk7AEVRFMW/VCJQFEWp4VQiUBRFqeFUIlAURanhVCJQFEWp4Qz+DqA8IiMjZWxsrL/DUBRFqVY2btx4XEoZdXZ7tUwEsbGxbNiwwd9hKIqiVCtCiGRP7aprSFEUpYZTiUBRFKWGU4lAURSlhlOJQFEUpYZTiUBRFKWGU4lAURSlhvNKIhBCTBNCHBVCbC/luBBCfCiE2COE2CqEuKLYsYeFELuLvh72RjyKoihK2XnriWA60PMcx3sBLYq+EoBPAIQQdYE3gSuBzsCbQog6XopJUS6MlLBhA6xdCwUF/o5GycyEJ56AunW1ryFDICPD31EFJK8kAinlWuDkOU65HZgpNeuAcCFEQ6AHsExKeVJKmQEs49wJRVF8Y/t2kiKfJbZzPXTXX0Os5ShJT//u76hqLqcTrrkGpk7V3vwzMuDzz+Hqq8Hh8Hd0AaeyxggaAweL/Zxa1FZauxshRIIQYoMQYsOxY8d8FqhSA9ntJF2dSMLJ90iWMUh0JMsYEhIvJWnMIX9HVzP98AMkJ4PNdqbNZoPUVPjuO//FFaAqKxEID23yHO3ujVJOllLGSSnjoqLctspQlPJbvpxhua9iJaREs5UQhg0P9lNQXvTPPzBnDvz5p9b9VR1s3QpWq3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H2jzsjJfLXnT0AKDyd+z9tMvmvpmU/s9fKZ/Lmx4gPFStVgsXhewKfXq4I25+CtCmU9hRD/CCH2CCGGejg+TgixuejrXyFEZrFjzmLHFnkjHk8a1W7EwA4DsRgt5zxPL/S0jgyMQeOv//6aqIeeQ4QfQAgXEQ1zSEryd1R+Fh5O/Nd3nvnEnu6F2UJK1XHffZ437JMS7rqr9NdJqe2f9O67WtdSDdsZtcKDxUIIPTAR6I5WjH69EGKRlPL0RiRSyueLnf800KHYJfKllJdXNI6ySLw1kcsbXM6EPyZwwnqCbFs2TpeTQmchAkGwIZiJvSdi1PupKpUXLdy1kAf/twT7gsTTexqdPFKbgY/ZAfUJWAlQTZvC5MmQkKDthwRaTYRZsyAqyvNrbDbo2RPWr9fGE4KDtR1bV63SttioASq8jkAI0QX4n5SyR9HPrwBIKd8r5fzfgDellMuKfs6VUl7Q6hRvVSg7mX+SiX9O5Me9PxITGsPzXZ6nc+ML3Ku9imo7sS1/D1sCWbFux2JiJMnJav2EEsBOntRqM+v12pt8WFjp544bp1VLO7u+88UXa7UYAmitkc9KVQoh7gZ6SikfK/r5IeBKKeVTHs5tCqwDmkgpnUVtDmAz4ABGSikXlHKfBCABICYmpmNycnKF4g50wSOCKXgtD0+9f0JIXK7A+eVWfMhuh59/1gZgr71W2z460Fx6KWzb5t5uscCWLdC8eeXH5CO+LFXp6R2ltOxyP/D1qSRQJKYosAeB8UKIiz29UEo5WUoZJ6WMiyrtEU857aLwiyAsxeOx6OhKDkapnv74Axo2hH794IEHtII5X3zh76i871wfhqvhzgvl4Y1EkAoUf2tpAhwu5dz7gdnFG6SUh4v+3AespuT4gVJO73V7D2P3t9w2tjMF2Xn3XfU0oJxHfr7WpXLiBGRna19Wq1aI3ge
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"print(\"Number of support vectors learnt:\", sum(svc.n_support_))\n",
"plt.scatter(X[:,0], X[:,1], c=y)\n",
"plt.scatter(svc.support_vectors_[:,0], svc.support_vectors_[:,1], c='b')"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Classification errors: 0\n"
]
}
],
"source": [
"predicted = svc.predict(X)\n",
"errors = sum([y[i] != predicted[i] for i in range(len(y))])\n",
"print(\"Classification errors:\", errors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Me lo aspettavo perchè le classi possono continuare ad essere separabili anche se proiettate in uno spazio vettoriale diverso"
]
}
],
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}