2012-02-28 08:05:16 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
|
|
|
|
SVM-Kernels
|
|
|
|
|
=========================================================
|
2013-06-06 18:30:42 +08:00
|
|
|
|
2012-02-28 08:05:16 +08:00
|
|
|
Three different types of SVM-Kernels are displayed below.
|
|
|
|
|
The polynomial and RBF are especially useful when the
|
2013-06-27 21:09:16 +08:00
|
|
|
data-points are not linearly separable.
|
2012-02-28 08:05:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-02-28 08:05:16 +08:00
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-02-28 08:05:16 +08:00
|
|
|
from sklearn import svm
|
|
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2012-02-28 08:05:16 +08:00
|
|
|
# Our dataset and targets
|
2011-12-18 19:39:53 +08:00
|
|
|
X = np.c_[
|
|
|
|
|
(0.4, -0.7),
|
|
|
|
|
(-1.5, -1),
|
|
|
|
|
(-1.4, -0.9),
|
|
|
|
|
(-1.3, -1.2),
|
|
|
|
|
(-1.1, -0.2),
|
|
|
|
|
(-1.2, -0.4),
|
2012-04-28 18:04:36 +08:00
|
|
|
(-0.5, 1.2),
|
|
|
|
|
(-1.5, 2.1),
|
|
|
|
|
(1, 1),
|
2011-12-18 19:39:53 +08:00
|
|
|
# --
|
2012-04-28 18:04:36 +08:00
|
|
|
(1.3, 0.8),
|
|
|
|
|
(1.2, 0.5),
|
|
|
|
|
(0.2, -2),
|
|
|
|
|
(0.5, -2.4),
|
|
|
|
|
(0.2, -2.3),
|
|
|
|
|
(0, -2.7),
|
2012-12-25 20:16:05 +08:00
|
|
|
(1.3, 2.1),
|
|
|
|
|
].T
|
2012-04-28 18:04:36 +08:00
|
|
|
Y = [0] * 8 + [1] * 8
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2012-02-28 08:05:16 +08:00
|
|
|
# figure number
|
|
|
|
|
fignum = 1
|
|
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
# fit the model
|
2012-02-28 08:05:16 +08:00
|
|
|
for kernel in ("linear", "poly", "rbf"):
|
2011-12-18 19:39:53 +08:00
|
|
|
clf = svm.SVC(kernel=kernel, gamma=2)
|
|
|
|
|
clf.fit(X, Y)
|
|
|
|
|
|
|
|
|
|
# plot the line, the points, and the nearest vectors to the plane
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(fignum, figsize=(4, 3))
|
|
|
|
|
plt.clf()
|
2012-04-28 18:04:36 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.scatter(
|
|
|
|
|
clf.support_vectors_[:, 0],
|
|
|
|
|
clf.support_vectors_[:, 1],
|
|
|
|
|
s=80,
|
2017-03-05 00:22:00 +08:00
|
|
|
facecolors="none",
|
|
|
|
|
zorder=10,
|
|
|
|
|
edgecolors="k",
|
|
|
|
|
)
|
|
|
|
|
plt.scatter(X[:, 0], X[:, 1], c=Y, zorder=10, cmap=plt.cm.Paired, edgecolors="k")
|
2021-10-07 16:13:00 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis("tight")
|
2011-12-18 19:39:53 +08:00
|
|
|
x_min = -3
|
|
|
|
|
x_max = 3
|
|
|
|
|
y_min = -3
|
|
|
|
|
y_max = 3
|
|
|
|
|
|
|
|
|
|
XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
|
|
|
|
|
Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
|
|
|
|
|
|
|
|
|
|
# Put the result into a color plot
|
|
|
|
|
Z = Z.reshape(XX.shape)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(fignum, figsize=(4, 3))
|
|
|
|
|
plt.pcolormesh(XX, YY, Z > 0, cmap=plt.cm.Paired)
|
|
|
|
|
plt.contour(
|
|
|
|
|
XX,
|
|
|
|
|
YY,
|
|
|
|
|
Z,
|
|
|
|
|
colors=["k", "k", "k"],
|
|
|
|
|
linestyles=["--", "-", "--"],
|
2014-05-15 10:35:13 +08:00
|
|
|
levels=[-0.5, 0, 0.5],
|
|
|
|
|
)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlim(x_min, x_max)
|
|
|
|
|
plt.ylim(y_min, y_max)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
2012-02-28 08:05:16 +08:00
|
|
|
fignum = fignum + 1
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|