自動化遞迴式特徵刪減
現在把這個遞迴流程自動化。將遞迴式特徵刪減器(RFE)包在我們的邏輯斯迴歸估計器外層,並傳入想要保留的特徵數量。
所有必要的函式與套件都已預先載入,特徵也已為你完成縮放。
本練習屬於課程
Python 的降維
練習說明
- 建立以
LogisticRegression()為估計器、並選擇 3 個特徵的 RFE。 - 列印特徵及其排名。
- 列印未被刪除的特徵。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the RFE with a LogisticRegression estimator and 3 features to select
rfe = ____(estimator=____, n_features_to_select=____, verbose=1)
# Fits the eliminator to the data
rfe.fit(X_train, y_train)
# Print the features and their ranking (high = dropped early on)
print(dict(zip(X.columns, rfe.____)))
# Print the features that are not eliminated
print(X.columns[rfe.____])
# Calculates the test set accuracy
acc = accuracy_score(y_test, rfe.predict(X_test))
print(f"{acc:.1%} accuracy on test set.")