遞迴特徵消除#

此範例示範如何使用遞迴特徵消除 (RFE) 來判斷個別像素對於分類手寫數字的重要性。RFE 會遞迴移除最不重要的特徵,根據其重要性分配等級,其中較高的 ranking_ 值表示較低的重要性。為了清楚起見,排名會同時使用藍色陰影和像素註釋來視覺化。如預期的,位於影像中心的像素往往比靠近邊緣的像素更具預測性。

Ranking of pixels with RFE (Logistic Regression)
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.pyplot as plt

from sklearn.datasets import load_digits
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler

# Load the digits dataset
digits = load_digits()
X = digits.images.reshape((len(digits.images), -1))
y = digits.target

pipe = Pipeline(
    [
        ("scaler", MinMaxScaler()),
        ("rfe", RFE(estimator=LogisticRegression(), n_features_to_select=1, step=1)),
    ]
)

pipe.fit(X, y)
ranking = pipe.named_steps["rfe"].ranking_.reshape(digits.images[0].shape)

# Plot pixel ranking
plt.matshow(ranking, cmap=plt.cm.Blues)

# Add annotations for pixel numbers
for i in range(ranking.shape[0]):
    for j in range(ranking.shape[1]):
        plt.text(j, i, str(ranking[i, j]), ha="center", va="center", color="black")

plt.colorbar()
plt.title("Ranking of pixels with RFE\n(Logistic Regression)")
plt.show()

腳本的總執行時間: (0 分鐘 3.104 秒)

相關範例

辨識手寫數字

辨識手寫數字

核密度估計

核密度估計

特徵聚合

特徵聚合

標籤傳播數字:展示效能

標籤傳播數字:展示效能

由 Sphinx-Gallery 產生的圖庫