自動再帰的特徴削除
では、この再帰的な処理を自動化してみましょう。ロジスティック回帰の推定器を再帰的特徴削除(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.")