I don't know how many features to keep. Also, code I copied from a tutorial uses rfecv.grid_scores_ and now fails with an AttributeError.
Answer 1: Use RFECV
RFECV runs RFE inside cross-validation. It measures the validation score for each number of features and picks the number with the best score.
from sklearn.feature_selection import RFECV
from sklearn.model_selection import StratifiedKFold
rfecv = RFECV(
estimator=LogisticRegression(max_iter=1000),
step=1,
cv=StratifiedKFold(5),
scoring="accuracy",
min_features_to_select=1,
n_jobs=-1,
)
rfecv.fit(X, y)
print("Optimal number of features:", rfecv.n_features_)
print("Selected:", X.columns[rfecv.support_].tolist())
Choose scoring to match your problem. For imbalanced classes, accuracy can be misleading, so consider "f1", "roc_auc", or "balanced_accuracy".
