Example: Support Vector Regression using linear and non-linear kernels

Support Vector Regression (SVR) using linear and non-linear kernels

Toy example of 1D regression using linear, polynomial and RBF kernels.

print(__doc__)

import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt

Generate sample data

X = np.sort(5 * np.random.rand(40, 1), axis=0)
y = np.sin(X).ravel()

Add noise to targets

y[::5] += 3 * (0.5 - np.random.rand(8))

Fit regression model

svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_lin = SVR(kernel='linear', C=1e3)
svr_poly = SVR(kernel='poly', C=1e3, degree=2)
y_rbf = svr_rbf.fit(X, y).predict(X)
y_lin = svr_lin.fit(X, y).predict(X)
y_poly = svr_poly.fit(X, y).predict(X)

look at the results

登录查看完整内容