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]:
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]:
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]:
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]:
In [ ]: