<jupyter_start><jupyter_text>```further: scaler.fit(), scaler.fit_transform(), get X_scalered with pipeline, scalar and scaler clf.support_vectors_, loss='hinge'``` ```1. Linear SVM Classification 2. Nonlinear SVM Classification 3. SVM Regression 4. Under the Hood```  1. SVM Classification: Linear<jupyter_code>import numpy as np 
import matplotlib.pyplot as plt
plt.style.use('tableau-colorblind10')
from sklearn.datasets import load_iris
iris = load_iris()
X = iris['data'][:, 2:]
y = iris['target']
y_2 = (y == 2).astype('int')

plt.scatter(X[:, 0], X[:, 1], c=y_2, cmap='tab20b', alpha=0.5)
plt.colorbar()
plt.show()

##'setosa', 'versicolor', 'virginica'
## petal length, petal widt
## svc = SVC(kernel='linear'), svc.support_vectors_
## svc = LinearSVC(), no 
from sklearn.pipeline import Pipeline 
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
from sklearn.svm import SVC

scaler_svc = Pipeline([
    ('scaler', StandardScaler()),
    ('linear_svc', LinearSVC(C=10, loss='hinge') )
])

scaler = StandardScaler()
X_scalered = scaler.fit_transform(X)
linear_svc = SVC(kernel='linear', C=10)
linear_svc.fit(X_scalered, y_2)

plt.scatter(X_scalered[:, 0], X_scalered[:, 1], c=y_2, cmap='tab20b', alpha=0.5)

ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()

xx = np.linspace(xlim[0], xlim[1], 50) 
yy = np.linspace(ylim[0], ylim[1], 50) 
XX, YY = np.meshgrid(xx, yy)
xy = np.c_[XX.ravel(), YY.ravel()]
Z = linear_svc.decision_function(xy).reshape(XX.shape)
ax.contour(XX, YY, Z, levels=[-2, 0, 2], 
           colors='k', alpha=0.6,  linestyles=['--', '-', '--'])
plt.show()
## np.c_: add column, np.vstack().T: stack vertically<jupyter_output><empty_output><jupyter_text>2. SVM Classification: Nonlinear(Polynomial)<jupyter_code>from sklearn.datasets import make_moons
from sklearn.preprocessing import PolynomialFeatures
X, y = make_moons(noise=0.15)
X.shape, y.shape
## a simple toy dataset: default n_samples = 100, noise = 0
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='tab20b', alpha=0.5)
plt.show()
poly_features = PolynomialFeatures(degree=3, include_bias=False)
X_poly = poly_features.fit_transform(X)
plt.scatter(X_poly[:, 0], X_poly[:, 2], c=y, cmap='tab20b', alpha=0.5)
plt.show()
poly_svc = Pipeline([
    ('poly', PolynomialFeatures(degree=3)),
    ('scaler', StandardScaler()),
    ('linear_svc', LinearSVC(C=10, loss='hinge'))
])
poly_kernel_svc = Pipeline([
    ('scaler', StandardScaler()),
    ('poly_svc', SVC(kernel='poly', degree=3, C=10, coef0=1))
])
## similarity features 
## add new features to seperate data

## rbf kernel: guassian radial basis function 

## string kernel: for DNA sequences or text documents 
rbf_kernel_scv = Pipeline([
    ('scaler', StandardScaler()),
    ('rbf_svc', SVC(kernel='rbf', C=0.001, gamma=5))
])<jupyter_output><empty_output><jupyter_text>3. SVM Regression<jupyter_code>## SVM for outlier detection<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1738241308793456, 'fraction_numerical': 0.026244035446489435, 'mean_word_length': 5.857476635514018, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '133856', 'license': 'MIT', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1138, 'n_words': 253, 'path': '05_support_vector_machine.ipynb', 'repo_name': 'fanjingwenvi/handson_ml2_note', 'size': '2934'}