CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Dimensionality Reduction in Python

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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.") 
Modifier et exécuter le code