Automatic Recursive Feature Elimination
Now let's automate this recursive process. Wrap a Recursive Feature Eliminator (RFE) around our logistic regression estimator and pass it the desired number of features.
All the necessary functions and packages have been pre-loaded and the features have been scaled for you.
This exercise is part of the course
Dimensionality Reduction in Python
Exercise instructions
- Create the RFE with a
LogisticRegression()
estimator and 3 features to select. - Print the features and their ranking.
- Print the features that are not eliminated.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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.")