Toggle Menu
Prev Up Next

scikit-learn 1.3.2
Other versions

Please cite us if you use the software.

Note

Go to the end to download the full example code or to run this example in your browser via Binder

# Recursive feature elimination

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

Note

See also Recursive feature elimination with cross-validation

    import matplotlib.pyplot as plt

    from sklearn.datasets import load_digits
    from sklearn.feature_selection import RFE
    from sklearn.svm import SVC

    # 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)

    # Plot pixel ranking
    plt.matshow(ranking, cmap=plt.cm.Blues)
    plt.colorbar()
    plt.title("Ranking of pixels with RFE")
    plt.show()

Total running time of the script: (0 minutes 2.449 seconds)

Gallery generated by Sphinx-Gallery