SVM : kernel trick¶

In [25]:
import numpy as np
import pandas as pa
dic = {'X': [118, 122, 123, 132, 135, 141, 145, 150, 156, 158, 163, 170, 172, 178],
      'Y': [1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1]}
data = pa.DataFrame(data=dic)
In [26]:
import matplotlib.pyplot as plt
%matplotlib inline

X = data['X']
y = data['Y']

fig, axes = plt.subplots()
plt.grid()

n = X.shape[0]
print(n)
for i in range(n):
    if y[i] == 1:
        color = 'r'
    else:
        color = 'b'
    axes.scatter(X[i],0,color=color)
14
No description has been provided for this image
In [27]:
def phi(x):
    return (x - 150) / 10, ((x - 150) / 10)**2
In [28]:
X1, X2 = phi(X)
X1
Out[28]:
0    -3.2
1    -2.8
2    -2.7
3    -1.8
4    -1.5
5    -0.9
6    -0.5
7     0.0
8     0.6
9     0.8
10    1.3
11    2.0
12    2.2
13    2.8
Name: X, dtype: float64
In [29]:
fig, axes = plt.subplots()
plt.grid()
for i in range(n):
    if y[i] == 1:
        color = 'r'
    else:
        color = 'b'
    axes.scatter(X1[i],X2[i],color=color)
No description has been provided for this image
In [ ]: