In [1]:
# (p. 148) Support Vector Machines (SVM)
import numpy as np
from sklearn import datasets
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC

iris = datasets.load_iris()
X = iris["data"][:, (2, 3)]  # petal length and width
y = (iris["target"] == 2).astype(np.float64) # only train Iris Virginica

svm_clf = Pipeline((
    ("scaler", StandardScaler()),
    ("linear_svc", LinearSVC(C=1, loss="hinge"))
))
svm_clf.fit(X, y)

svm_clf.predict([[5.5, 1.7]]) # 0 / 1 = no / yes


Out[1]:
array([ 1.])

In [2]:
# (p. 149) nonlinear SVM 
# Erratum: should use moon dataset as per the book, but does not
from sklearn.preprocessing import PolynomialFeatures

poly_svm_cfl = Pipeline((
    ("poly_features", PolynomialFeatures(degree=3)),
    ("scaler", StandardScaler()),
    ("svm_cfl", LinearSVC(C=10, loss="hinge"))
))

poly_svm_cfl.fit(X, y)
poly_svm_cfl.predict([[5.5, 1.7]])


Out[2]:
array([ 1.])

In [3]:
# (p. 150) "kernel trick"
from sklearn.svm import SVC

poly_kernel_svm_cfl = Pipeline((
    ("scaler", StandardScaler()),
    ("svm_clf", SVC(kernel="poly", degree=3, coef0=1, C=5))
))

poly_kernel_svm_cfl.fit(X, y)
poly_kernel_svm_cfl.predict([[5.5, 1.7]])


Out[3]:
array([ 1.])

In [4]:
rbf_kernel_svm_clf = Pipeline((
    ("scaler", StandardScaler()),
    ("svm_clf", SVC(kernel="rbf", gamma=5, C=0.001))
))
rbf_kernel_svm_clf.fit(X, y)


Out[4]:
Pipeline(steps=(('scaler', StandardScaler(copy=True, with_mean=True, with_std=True)), ('svm_clf', SVC(C=0.001, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma=5, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False))))

In [ ]: