<jupyter_start><jupyter_text>A recursive feature elimination example showing the relevance of pixels in a digit classification task. Note: See also [Recursive feature elimination with cross-validation](http://scikit-learn.org/stable/auto_examples/feature_selection/plot_rfe_with_cross_validation.htmlsphx-glr-auto-examples-feature-selection-plot-rfe-with-cross-validation-py)  New to Plotly?Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).You can set up Plotly to work in [online](https://plot.ly/python/getting-started/initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/start-plotting-online).We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!  Version<jupyter_code>import sklearn
sklearn.__version__<jupyter_output><empty_output><jupyter_text>Imports This tutorial imports [SVC](http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.htmlsklearn.svm.SVC), [load_digits](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.htmlsklearn.datasets.load_digits) and [RFE](http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.htmlsklearn.feature_selection.RFE).<jupyter_code>print(__doc__)

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<jupyter_output>Automatically created module for IPython interactive environment<jupyter_text>Calculations<jupyter_code># Load the digits dataset
digits = load_digits()
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)<jupyter_output><empty_output><jupyter_text>Plot Pixel Ranking<jupyter_code>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
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)
py.iplot(fig)
from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'Recursive feature elimination.ipynb', 'scikit-learn/plot-rfe-digits/', 'Recursive Feature Elimination | plotly',
    ' ',
    title = 'Recursive Feature Elimination | plotly',
    name = 'Recursive Feature Elimination',
    has_thumbnail='true', thumbnail='thumbnail/rfe-digits.jpg', 
    language='scikit-learn', page_type='example_index',
    display_as='feature_selection', order=2,
    ipynb= 'Diksha_Gabha/3075')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13197566781274794, 'fraction_numerical': 0.010314731552499339, 'mean_word_length': 5.356302521008403, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '303415', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1265, 'n_words': 223, 'path': '_posts/scikit/Recursive-feature-elimination/Recursive feature elimination.ipynb', 'repo_name': 'bmb804/documentation', 'size': '3781'}