Recursive Feature Elimination

medium
Unit 16 · Semi Supervised0/3 to milestone

Recursive feature elimination (RFE) fits a model, drops the least important feature, and repeats. With a linear model on standardised features, importance is the absolute coefficient.

Implement:

  • ridge_coefs(X, y, lam): standardise X's columns (zero mean, unit population std; a constant column stays all zeros), centre y, and return the ridge coefficients (X⊤X+λI)−1X⊤y(X^\top X + \lambda I)^{-1}X^\top y
  • rfe(X, y, n_keep, lam=1.0): return (kept, eliminated): the sorted original indices of the surviving features, and the features in the order they were removed. Each round, refit on the remaining columns and remove the one with the smallest |coefficient| (ties: lowest original index).
#feature-selection#regularization#linear-models2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Recursive Feature Elimination · TechGradify