Show Sidebar Hide Sidebar

SVM Exercise in Scikit-learn

A tutorial exercise for using different SVM kernels.

This exercise is used in the Using kernels part of the Supervised learning: predicting an output variable from high-dimensional observations section of the A tutorial on statistical-learning for scientific data processing.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
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

In [2]:
print(__doc__)

import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, svm
Automatically created module for IPython interactive environment

Calculations

In [3]:
iris = datasets.load_iris()
X = iris.data
y = iris.target

X = X[y != 0, :2]
y = y[y != 0]

n_sample = len(X)

np.random.seed(0)
order = np.random.permutation(n_sample)
X = X[order]
y = y[order].astype(np.float)

X_train = X[:.9 * n_sample]
y_train = y[:.9 * n_sample]
X_test = X[.9 * n_sample:]
y_test = y[.9 * n_sample:]

Plot Results

In [4]:
def matplotlib_to_plotly(cmap, pl_entries):
    h = 1.0/(pl_entries-1)
    pl_colorscale = []
    
    for k in range(pl_entries):
        C = map(np.uint8, np.array(cmap(k*h)[:3])*255)
        pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
        
    return pl_colorscale

cmap = matplotlib_to_plotly(plt.cm.Paired, 4)

def data_to_plotly(Z):
        l = []
        p = Z>0
        for i in range(0, len(p)):
            k = []
            for j in range(0, len(p[i])):
                k.append(int(p[i][j]))
            l.append(k)
        return l
    
fig = tools.make_subplots(rows=1, cols=3,
                          subplot_titles=('linear', 'rbf', 'poly')
                         )
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]  [ (1,3) x3,y3 ]

In [5]:
for fig_num, kernel in enumerate(('linear', 'rbf', 'poly')):
    clf = svm.SVC(kernel=kernel, gamma=10)
    clf.fit(X_train, y_train)

    x_min = X[:, 0].min()
    x_max = X[:, 0].max()
    y_min = X[:, 1].min()
    y_max = X[:, 1].max()
    
    x_ = np.linspace(x_min, x_max, 200)
    XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
    Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
    
    # Put the result into a color plot
    Z = Z.reshape(XX.shape)
        
    p1 = go.Contour(x=x_, y=YY[0], 
                    z=data_to_plotly(Z), 
                    colorscale=cmap,
                    showscale=False)
    
    p2 = go.Scatter(x=X[:, 0], y=X[:, 1], 
                    mode='markers',
                    showlegend=False,
                    marker=dict(color=X[:, 0],
                                colorscale=cmap)
                   )
    
    fig.append_trace(p1, 1, fig_num+1)
    fig.append_trace(p2, 1, fig_num+1)

    # Circle out the test data
    p3 = go.Scatter(x=X_test[:, 0], 
                    y=X_test[:, 1], 
                    showlegend=False,
                    mode='markers',
                    marker=dict(color=X[:, 0], colorscale=cmap,
                                showscale=False,
                                size=10, line=dict(width=1, color='black')
                               )
                   )
    fig.append_trace(p3, 1, fig_num+1)
fig['layout'].update(title='SVM Excercise', hovermode='closest')
In [6]:
for i in map(str, range(1, 4)):
    x = 'xaxis' + i
    y = 'yaxis' + i
    fig['layout'][x].update(ticks='', showticklabels=False,
                            showgrid=False, zeroline=False)
    fig['layout'][y].update(ticks='', showticklabels=False,
                            showgrid=False, zeroline=False)



py.iplot(fig)
Out[6]:
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.