Show Sidebar Hide Sidebar

Recursive Feature Elimination in Scikit-learn

A recursive feature elimination example showing the relevance of pixels in a digit classification task.

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


This tutorial imports SVC, load_digits and RFE.

In [2]:

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

import numpy as np
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn.feature_selection import RFE
import matplotlib.pyplot as plt
Automatically created module for IPython interactive environment


In [3]:
# Load the digits dataset
digits = load_digits()
X = digits.images.reshape((len(digits.images), -1))
y =

# Create the RFE object and rank each pixel
svc = SVC(kernel="linear", C=1)
rfe = RFE(estimator=svc, n_features_to_select=1, step=1), y)
ranking = rfe.ranking_.reshape(digits.images[0].shape)

Plot Pixel Ranking

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
In [5]:
trace = go.Heatmap(z=ranking, 
                   colorscale=matplotlib_to_plotly(, len(ranking))

layout = go.Layout(title="Ranking of pixels with RFE",
fig = go.Figure(data=[trace], layout=layout)
In [6]:
Still need help?
Contact Us

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