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?¶

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 SVC, load_digits and RFE.

In [2]:
print(__doc__)

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

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

### Calculations¶

In [3]:
X = digits.images.reshape((len(digits.images), -1))
y = digits.target

# 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)
rfe.fit(X, 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(plt.cm.Blues, len(ranking))
)

layout = go.Layout(title="Ranking of pixels with RFE",
yaxis=dict(autorange='reversed')
)
fig = go.Figure(data=[trace], layout=layout)
In [6]:
py.iplot(fig)
Out[6]:
Still need help?