import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib inline
SVM with RBF kernel Gamma Factor
SVM with RBF kernel Gamma Factor
= np.array([
X 0, 0],
[-1, 0],
[1, 0],
[0, 1],
[0, -1],
[-2, 0],
[2, 0],
[0, -2],
[0, 2]
[
])= np.array([1, 1, 1, 1, 1, -1, -1, -1, -1]) y
0], X[:, 1], c=y, cmap=cm.Paired)
plt.scatter(X[:, 'equal') plt.gca().set_aspect(
import pandas as pd
= pd.DataFrame(X, columns=['x1', 'x2'])
df 'y'] = y
df[ df
x1 | x2 | y | |
---|---|---|---|
0 | 0 | 0 | 1 |
1 | -1 | 0 | 1 |
2 | 1 | 0 | 1 |
3 | 0 | 1 | 1 |
4 | 0 | -1 | 1 |
5 | -2 | 0 | -1 |
6 | 2 | 0 | -1 |
7 | 0 | -2 | -1 |
8 | 0 | 2 | -1 |
from sklearn.metrics.pairwise import rbf_kernel
import seaborn as sns
= 0.1 # parameter for RBF kernel
gamma = rbf_kernel(X, gamma=gamma)
K =True, cmap=cm.viridis) sns.heatmap(K, annot
<AxesSubplot:>
= [0.01, 0.1, 1]
gammas = plt.subplots(1, 3, figsize=(16, 4))
fig, axes for ax, gamma in zip(axes.ravel(), gammas):
= rbf_kernel(X, gamma=gamma)
K # Annotate to 1 decimal digit and use min = 0.0 and max =1.0
=True, cmap=cm.viridis, ax=ax, fmt='.1f', vmin=0.0, vmax=1.0)
sns.heatmap(K, annot'gamma = {}'.format(gamma)) ax.set_title(
# Compare with the linear kernel
from sklearn.metrics.pairwise import linear_kernel, polynomial_kernel
= linear_kernel(X)
K =True, cmap=cm.viridis, fmt='.1f') sns.heatmap(K, annot
<AxesSubplot:>
# Compare with the polynomial kernel
= polynomial_kernel(X, degree=2)
K =True, cmap=cm.viridis, fmt='.1f') sns.heatmap(K, annot
<AxesSubplot:>