Show Sidebar Hide Sidebar

# Support Vector Regression Using Linear and Non-Linear Kernels in Scikit-learn

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

#### New to Plotly?¶

You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version¶

In [1]:
import sklearn
sklearn.__version__

Out[1]:
'0.18.1'

### Imports¶

This tutorial imports SVR.

In [2]:
print(__doc__)

import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np
from sklearn.svm import SVR

Automatically created module for IPython interactive environment


### Calculations¶

Generate sample data

In [3]:
X = np.sort(5 * np.random.rand(40, 1), axis=0)
y = np.sin(X).ravel()


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


Fit regression model

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


### Plot Results¶

In [6]:
def data_to_plotly(x):
k = []

for i in range(0, len(x)):
k.append(x[i][0])

return k

In [7]:
lw = 2
p1 = go.Scatter(x=data_to_plotly(X), y=y,
mode='markers',
marker=dict(color='darkorange'),
name='data')

p2 = go.Scatter(x=data_to_plotly(X), y=y_rbf,
mode='lines',
line=dict(color='navy', width=lw),
name='RBF model')

p3 = go.Scatter(x=data_to_plotly(X), y=y_lin,
mode='lines',
line=dict(color='cyan', width=lw),
name='Linear model')

p4 = go.Scatter(x=data_to_plotly(X), y=y_poly,
mode='lines',
line=dict(color='cornflowerblue', width=lw),
name='Polynomial model')

layout = go.Layout(title='Support Vector Regression',
hovermode='closest',
xaxis=dict(title='data'),
yaxis=dict(title='target'))

fig = go.Figure(data=[p1, p2, p3, p4], layout=layout)
py.iplot(fig)

Out[7]:
Still need help?