# 深度探索监督学习：支持向量机

``````

In [1]:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

# 使用seaborn的一些默认配置
import seaborn as sns; sns.set()

``````

## 支持向量机

``````

In [2]:

from sklearn.datasets.samples_generator import make_blobs

X, y = make_blobs(n_samples=50, centers=2,
random_state=0, cluster_std=0.60)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring');

``````
``````

``````

``````

In [3]:

xfit = np.linspace(-1, 3.5)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')

for m, b in [(1, 0.65), (0.5, 1.6), (-0.2, 2.9)]:
#绘制分割线
plt.plot(xfit, m * xfit + b, '-k')

plt.xlim(-1, 3.5);

``````
``````

``````

### 支持向量机：最大化边界

``````

In [4]:

xfit = np.linspace(-1, 3.5)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')

for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:
yfit = m * xfit + b
#绘制分割线
plt.plot(xfit, yfit, '-k')
#绘制分割线两侧的区域
plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none', color='#AAAAAA', alpha=0.4)

plt.xlim(-1, 3.5);

``````
``````

``````

#### 生成一个支持向量机

``````

In [5]:

from sklearn.svm import SVC  # "Support Vector Classifier"

clf = SVC(kernel='linear')
clf.fit(X, y)

``````
``````

Out[5]:

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma='auto', kernel='linear',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)

``````

``````

In [6]:

def plot_svc_decision_function(clf, ax=None):
"""绘制一个 2D SVC 的决策函数"""
if ax is None:
ax = plt.gca()
x = np.linspace(plt.xlim()[0], plt.xlim()[1], 30)
y = np.linspace(plt.ylim()[0], plt.ylim()[1], 30)
Y, X = np.meshgrid(y, x)
P = np.zeros_like(X)
for i, xi in enumerate(x):
for j, yj in enumerate(y):
P[i, j] = clf.decision_function([[xi, yj]])
# 绘制边界
ax.contour(X, Y, P, colors='k',
levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'])

``````
``````

In [7]:

plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')
plot_svc_decision_function(clf);

``````
``````

``````

``````

In [8]:

plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')
plot_svc_decision_function(clf)
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
s=200, alpha=0.3);

``````
``````

``````

``````

In [9]:

from ipywidgets import interact

def plot_svm(N=10):
X, y = make_blobs(n_samples=200, centers=2,
random_state=0, cluster_std=0.60)
X = X[:N]
y = y[:N]
clf = SVC(kernel='linear')
clf.fit(X, y)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')
plt.xlim(-1, 4)
plt.ylim(-1, 6)
plot_svc_decision_function(clf, plt.gca())
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
s=200, alpha=0.3)

interact(plot_svm, N=[10, 200], kernel='linear');

``````
``````

var element = \$('#8a1cd639-6a1d-4d49-996d-0f0f676b55df');

{"model_id": "820714b452aa41dfb3864a29031bd8f1"}

``````

#### 进一步探索：核方法

``````

In [10]:

from sklearn.datasets.samples_generator import make_circles

X, y = make_circles(100, factor=.1, noise=.1)

clf = SVC(kernel='linear').fit(X, y)

plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')
plot_svc_decision_function(clf);

``````
``````

``````

``````

In [11]:

r = np.exp(-(X[:, 0] ** 2 + X[:, 1] ** 2))

``````

``````

In [12]:

from mpl_toolkits import mplot3d

def plot_3D(elev=30, azim=30):
ax = plt.subplot(projection='3d')
ax.scatter3D(X[:, 0], X[:, 1], r, c=y, s=50, cmap='spring')
#     ax.view_init(elev=elev, azim=azim)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('r')

# interact(plot_3D, elev=[-90, 90], azip=(-180, 180));
plot_3D()

``````
``````

``````

``````

In [13]:

clf = SVC(kernel='rbf')
clf.fit(X, y)

plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')
plot_svc_decision_function(clf)
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
s=200, alpha=0.3);

``````
``````

``````

``````

In [ ]:

``````