Concepts / Recursive Feature Elimination in Practice

Recursive Feature Elimination in Practice

Iterative feature selection builds a series of models, each with a different number of features, instead of making one decision from a single pass.

  • Programming
Interactive lab

Try it: The Call Stack

How the call stack keeps track of function calls: each call pushes a frame with its own arguments and local variables, and each return pops it.

How it works

  1. Calling a function pushes a new frame on top of the stack; the caller pauses.
  2. The frame holds that call's parameters and local variables.
  3. When the function returns, its frame is popped and the return value goes back to the paused caller.
  4. A recursive function pushes one frame per call until the base case, then the frames unwind in reverse order.

Default run (17 steps): Program starts: main is about to call factorial(4). … The stack is empty again. Result: 24. Deepest point: 4 frames, 4 calls in total.

Educational simulation

Loading the simulation…

Why One Pass Is Not Always Enough

When a dataset has many features, a common task is to keep the useful ones and drop the rest. Earlier approaches make this choice in a single pass over the data: the decision about each feature comes from one calculation or one trained model, and then the process is finished. Iterative feature selection takes a different route. Instead of making one decision, it builds a whole series of models, each using a different number of features, and lets what each model reveals guide the next step.

What do you think happens?

Suppose a model trained on four features reports these importance scores: A 0.40, B 0.10, C 0.30, D 0.20. If one feature must be removed, which one should go first?

  • A

  • B

  • C

  • D

Reveal answer
Answer:

B

B has the lowest importance score, so it is the least important feature in this round. Iterative methods use exactly this kind of ranking to decide what to discard, and the rest of this article shows how that decision is repeated.

Univariate Testing Versus Single-Model Selection

Univariate testing looks at each feature on its own, without using any model. Each feature is judged separately, so the feature's relationship to the target is measured in isolation from the other features. Model-based selection takes a step toward considering the features together: it trains one model and uses that model to decide which features to keep. In both cases, the selection decision comes from a single pass over the data.

Iterative feature selection asks a different question. Rather than deciding once, it builds a series of models, and each model in the series works with a different number of features. The selection process moves through that series, using what each model shows to choose the next step. The diagram below places the three approaches side by side so the difference in how many models each one trains is easy to see.

Univariate testingeach feature scored alone,no modelModel-based selectionone model trained, onedecisionIterative selectionmany models, each decisionguides the next
How does a method that retrains over many feature subsets differ from one that scores each feature once or makes one decision from a single model?

Two Directions: Adding or Removing Features

Iterative feature selection has two basic methods. They start from opposite ends of the feature set and move toward a smaller or larger subset, depending on the method. One method starts with no features and adds them one by one. The other starts with all features and removes them one by one. Both keep building models until a stopping criterion is reached, and the choice between them determines where the search begins.

FeatureAdding one by oneRemoving one by one (RFE)
Starting pointNo featuresAll features
Direction of movementGrows the feature setShrinks the feature set
What each round decidesWhich feature to add nextWhich least important feature to discard
Ends whenA stopping criterion is reachedA prespecified number of features remain

The two basic directions of iterative selection start from opposite ends of the feature set.

Recursive Feature Elimination Step by Step

Recursive feature elimination (RFE) is a removal-based iterative method. It begins with all features and builds a model using them. That model is then used to find the least important feature, which is discarded. A new model is built using all of the remaining features except the one just discarded. The cycle repeats: build a model, discard the least important feature, build again. RFE stops once only a prespecified number of features are left.

  1. 1
    Start with the full set of features.
  2. 2
    Build a model using the current features.
  3. 3
    Use the model to determine the importance of each current feature.
  4. 4
    Discard the least important feature.
  5. 5
    Check whether the prespecified number of features has been reached. If not, return to the model-building step with the remaining features.
  6. 6
    When the target number remains, stop and keep the surviving features.

The important point is that the output of each model is not the end of the process. Its importance scores become the input to a decision that changes the feature set, and the changed feature set becomes the input to the next model. Data flows around this loop until the stopping condition is met. The flowchart below shows that loop.

Fit modelcurrent features onlyRank featuresby importance scoreTarget count reached?Discard leastimportantremove one featureKeep remainingfeaturesimportance scoresranked listnorebuild on remainingyes
How does the model's output turn into a ranking, and how does that ranking decide which feature is removed next?

Tracing Four Features Down to Two

The example below is an illustrative one written for this article. The features are named A, B, C, and D, and the importance scores are invented to show the mechanics. The goal is to reduce four features to two. Follow what remains after each round, and notice that the scores are recalculated from scratch each time a model is rebuilt.

Worked example

Four Features Reduced to Two

Starting with features A, B, C, and D, apply recursive feature elimination until two features remain. Each round, build a model on the current features, rank them by importance, and discard the least important one.

  1. 1
    Round 1: all four features

    The model is built on A, B, C, and D. Its importance scores are A 0.40, B 0.10, C 0.30, and D 0.20. The lowest is B at 0.10, so B is discarded. Four features are still more than the target of two.

  2. 2
    Round 2: three features remain

    A new model is built using only A, C, and D. Removing B changed the situation, so the scores are recalculated: A 0.45, C 0.25, and D 0.30. The lowest is C at 0.25, so C is discarded. Three features became two, but two is the target, so the loop would stop here.

  3. 3
    Stop at the target count

    Two features remain: A and D. The process ended because the prespecified number of features was reached. Only two models were trained for this trace in the count of rounds that removed a feature, and each one was fit fresh on the features still in play.

Result

The surviving feature set is A and D. Feature B was removed first, then feature C, and each removal was guided by a model rebuilt on the features that remained.

Round 1 modelA, B, C, D (4 features)Drop lowest: Bimportance 0.10Round 2 modelA, C, D (3 features)Drop lowest: Cimportance 0.25Stop: A and D2 features remainrank importancesremove B, rebuildrank importancesremove C, target reached
After each round, which features remain, which one gets dropped, and how does the feature set shrink until the target count is reached?

Why the Selecting Model Must Rank Features

RFE can discard only the least important feature if the model can say which features are important. The requirement is that the model used for selection has some way to determine feature importance, the same requirement that applies to model-based selection. A model that cannot rank its inputs gives RFE nothing to act on, because the central step of the loop, discarding the weakest feature, has no basis for choosing.

The source's example uses a random forest model, the same model used earlier in the discussion of model-based selection. Choosing the model is therefore part of designing the method. The selection is only as informed as the importance measure the model provides, so the quality of the ranking directly controls the quality of every removal decision.

Why Iterative Selection Costs More

Because a series of models is built, iterative methods are much more computationally expensive than the methods discussed previously. A univariate test may examine each feature once. A single model-based selection trains one model. An iterative method trains many models, and each one must be fit before the search can decide what to do next. The total work grows with the number of models in the series, not with a single training run.

Common Misreadings of RFE

  • Common mistake

    Treating RFE as a single ranking of features done once

    What it looks like

    Ranking features on the full set in round one and then removing them in that fixed order without rebuilding the model.

    Why it fails

    Importance can change once other features are removed, so the ranking from the first round may not hold in later rounds.

    Do this instead

    Rebuild the model after each removal and compute importance again on the remaining features.

  • Common mistake

    Assuming any model can drive RFE

    What it looks like

    Choosing a model that provides no importance measure and expecting RFE to still know which feature to discard.

    Why it fails

    RFE can discard the least important feature only if the model can say which features are important.

    Do this instead

    Pick a selecting model that can rank its features, such as the random forest used in the source's example.

  • Common mistake

    Confusing the direction of iterative methods

    What it looks like

    Describing RFE as a method that starts with no features and adds them one at a time.

    Why it fails

    RFE starts with all features and removes them. Adding one by one is the opposite direction.

    Do this instead

    Remember that RFE is a removal-based method that begins with the full feature set.

  • Common mistake

    Forgetting that iterative methods train many models, so their cost grows with the number of models in the series

Practice Tracing a New Feature Set

MEDIUM

Using the same four-feature setup as the worked example, suppose the round 2 scores are A 0.45, C 0.25, and D 0.30. If the prespecified target were one feature instead of two, which feature would RFE discard next, and which feature would remain at the end?

Hints
  • Round 2 ended by discarding C, so C is no longer in the set when the next round begins.
  • Only A and D remain after C is removed. Compare their importance scores to find the least important one.
  • Once the second feature is discarded, the single remaining feature is the answer for a target of one.

Key Takeaways

  • Iterative feature selection builds a series of models, each with a different number of features, instead of making one decision from a single pass as univariate testing and single-model selection do.
  • The two basic iterative methods start from opposite ends: adding features one by one from none, or removing features one by one from all.
  • Recursive feature elimination starts with all features, builds a model, discards the least important feature, and rebuilds until a prespecified number of features remain.
  • RFE depends on the selecting model being able to rank features, and rebuilding the model each round matters because importance can change as features are removed.
  • Iterative methods are much more computationally expensive because they train many models, and the total work grows with the number of models in the series.

Related concepts

More concepts starting with R