Show Sidebar Hide Sidebar

SVM with Custom Kernel in Scikit-learn

Simple usage of Support Vector Machines to classify a sample. It will plot the decision surface and the support vectors.

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!


In [1]:
import sklearn


In [2]:

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

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


In [3]:
# import some data to play with
iris = datasets.load_iris()
X =[:, :2]  # we only take the first two features. We could
                      # avoid this ugly slicing by using a two-dim dataset
Y =

def my_kernel(X, Y):
    We create a custom kernel:

                 (2  0)
    k(X, Y) = X  (    ) Y.T
                 (0  1)
    M = np.array([[2, 0], [0, 1.0]])
    return, M), Y.T)

h = .02  # step size in the mesh

# we create an instance of SVM and fit out data.
clf = svm.SVC(kernel=my_kernel), Y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto',
  kernel=<function my_kernel at 0x7fb4606bfed8>, max_iter=-1,
  probability=False, random_state=None, shrinking=True, tol=0.001,
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(, 4)

Plot Results

Plot the decision boundary. For that, we will assign a color to each point in the mesh [x_min, x_max]x[y_min, y_max].

In [5]:
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
x_ = np.arange(x_min, x_max, h)
y_ =  np.arange(y_min, y_max, h)
xx, yy = np.meshgrid(x_, y_)
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
In [6]:
trace1 = go.Heatmap(x=x_, y=y_, z=Z,

trace2 = go.Scatter(x=X[:, 0], y=X[:, 1], 
                                line=dict(color='black', width=1)))

layout = go.Layout(title="3-Class classification using Support Vector Machine with custom kernel")
fig = go.Figure(data= [trace1, trace2], layout=layout)
In [7]:
Still need help?
Contact Us

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