devnotes.

Comparison and Best Practices

Filter vs. wrapper vs. embedded

AspectFilterWrapperEmbedded
Uses a model?NoYes (repeatedly)Yes (built in)
ComputationLowHighMedium
Captures interactionsNoYesPartly
Overfitting riskLowHigherMedium
Best forHuge feature sets, first passSmall or medium feature setsGeneral purpose

A practical recipe

  1. Clean and split the data first.
  2. Remove constant and duplicated features.
  3. Drop one of each highly correlated pair.
  4. Use a filter method to cut the feature set to a manageable size.
  5. Use an embedded method (Lasso or tree importances) to rank features.
  6. Optionally use a wrapper (RFE) for the final subset.
  7. Validate with cross-validation on the training set.
  8. Confirm the final choice on the untouched test set.

Put selection inside a pipeline

Doing selection inside the training folds avoids leakage.

from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

pipe = Pipeline([
    ("impute", SimpleImputer(strategy="median")),
    ("scale", StandardScaler()),
    ("select", SelectKBest(f_classif, k=3)),
    ("model", LogisticRegression(max_iter=1000)),
])

print(cross_val_score(pipe, X_train, y_train, cv=5).mean())
pipe.fit(X_train, y_train)
print("Test accuracy:", pipe.score(X_test, y_test))

Best practices

  1. Do selection on the training data only.
  2. Start with cheap filters, then refine with embedded or wrapper methods.
  3. Combine statistical scores with domain knowledge.
  4. Compare model performance before and after selection.
  5. Remember that correlation does not imply causation.
  6. Prefer stable selections: check that the same features are chosen across folds.
  7. Record which features were removed and why.

When not to select features

  • The dataset is small and already has few features.
  • The model already regularizes strongly (for example, gradient boosting or ridge) and performs well.
  • Interpretability requires keeping all domain-relevant features.

Key takeaways

  • Fewer, better features generally give simpler, faster and more robust models.
  • Filter = fast screening, wrapper = accurate but costly, embedded = built into training.
  • Always fit selectors on the training data only.
Comparison and Best Practices - Feature Selection | Data Preparation | DevNotes