Univariate Feature Selection
The selection model is a tool for judging features, and the final model is the one you deploy.
Why Keeping Every Column Can Hurt
A supervised dataset often contains more columns than a useful model needs. Some features carry real signal about the target, while others add noise, duplicate information, or simply make the model harder to train. A common way to deal with this is to let a supervised machine learning model do the sorting. Instead of judging each feature on its own, you fit a model and ask it how much each feature contributes. The features can then be ranked by that measure, and only the strongest ones kept.
As you read, keep one question in mind: what does a model need to hand back so that we can rank its features? The answer shapes the rest of this article. We will first look at the general idea, then at the two families of models the source highlights, and finally at the transformer that performs the selection in code.
What a Selection Model Must Return
Model-based feature selection uses a supervised model to judge each feature's importance and keeps only the most important ones.
The phrase keeps only the most important ones describes the final goal, but the mechanism has two distinct parts. First, a model produces a measure of importance for every feature. Second, those measures are used to rank the features and decide which to retain. The source states the requirement for the first part directly: the selection model needs to provide some measure of importance for each feature, so that the features can be ranked by that measure. If a candidate model cannot produce such a measure, it cannot drive selection in this way.
Tracing a Feature Matrix Through Selection
Follow one feature matrix from start to finish. The selection model does its work on the full feature set, producing one importance measure per feature. The ranking step then turns those measures into an ordering. Only after that does the selection decision narrow the set. The final modeling step receives the reduced set, which may or may not be the same kind of model that performed the ranking.
Ranking three features by a model's importance measure
A selection model has produced one importance measure for each of three features, A, B, and C. The measures are 0.40, 0.90, and 0.10. Which features are ranked highest, and which would be kept if we retain the top two?
- 1Pair each feature with its measure
A has 0.40, B has 0.90, and C has 0.10.
- 2Sort by measure, largest first
The order becomes B (0.90), then A (0.40), then C (0.10).
- 3Apply the keep rule
Keeping the top two retains B and A, and drops C.
The ranking is B, A, C, and the reduced set is B and A. This illustrative example uses invented numbers to show the mechanism, not values from a real dataset.
Reading Importance From Decision Trees
Decision trees and decision tree-based models provide a feature_importances_ attribute. The source describes this attribute as one that directly encodes the importance of each feature. That phrase matters: for these models, you do not need to derive an importance score from some other quantity. After the model is fitted, the importance values are available as an attribute, ready to be ranked.
The attribute is accessed on the fitted model object, not on the raw data. Each position in the importance array corresponds to a column of X, so index 0 refers to the first feature, index 1 to the second, and so on. Ranking the features means sorting these positions by their values.
- Fit the tree on the full feature matrix X and target y
- feature_importances_ is read from the fitted object, not from the data
- argsort with [::-1] gives column positions from most to least important
The code shows the attribute being read rather than any particular set of numbers. Run it on your own dataset to see the values for that data.
Reading Importance From Linear Coefficients
Linear models take a different route. They do not expose a feature_importances_ attribute. Instead, they have coefficients, and these can also be used to capture feature importances by considering their absolute values. A coefficient is a signed number, positive or negative. Its sign tells you the direction of a feature's effect, but for ranking importance you care about how large the effect is, so the sign is set aside by taking the absolute value.
Here $w_{j}$ is the coefficient for feature $j$, and the importance score used for ranking is $|w_{j}|$. Features with larger absolute coefficients are ranked as more important under this measure.
Turning signed coefficients into a ranking
A linear model has coefficients +0.20 for feature A, -1.50 for feature B, and +0.05 for feature C. Rank the features by importance using absolute values.
- 1Take absolute values
The absolute values are 0.20 for A, 1.50 for B, and 0.05 for C.
- 2Notice the sign is discarded
B's coefficient is negative, but its magnitude of 1.50 is the largest, so it counts as the most important.
- 3Sort by magnitude
The order is B, then A, then C.
The ranking is B, A, C. A large negative coefficient counts as important because its effect on the prediction is large, even though its direction is opposite. These coefficient values are invented for illustration.
The source also connects linear models to sparsity. Linear models with an L1 penalty learn sparse coefficients, meaning that only a small subset of features receives nonzero weight. That sparsity can be viewed as a form of feature selection built into the model itself. It can also serve as a preprocessing step: the features with nonzero coefficients are kept and passed on to another model.
Selecting Features With SelectFromModel
Computing importance scores is only half the job. To use model-based feature selection in a workflow, the SelectFromModel transformer does the rest. In the scikit-learn style, a transformer is an object that learns something from training data and then transforms data. Here the thing it learns is which features pass an importance test, and the transformation removes the features that fail.
- The wrapped estimator supplies the importance values
fit_transformlearns which features pass and returns the reduced matrix- Comparing shapes shows how many columns were removed
The first printed shape is the original matrix, and the second shows the reduced one. The number of rows does not change, because the transformer removes features, not observations. The decision step depends on a threshold, and the exact threshold options are part of the transformer's configuration. Read the documentation for the version you have installed, and run the code to see how many columns were kept for your dataset.
One Judge or Many: Univariate Versus Model-Based
The source contrasts model-based selection with univariate selection. Univariate methods judge each feature separately, one at a time. Model-based selection instead considers all features at once, because the model is fitted on the full feature set. This is the reason the source says model-based selection can capture interactions, provided the model is able to capture them.
| Aspect | Univariate selection | Model-based selection |
|---|---|---|
| Features considered at once | One feature at a time | All features together |
| What drives the judgment | Each feature's own relationship to the target | A fitted model's importance measure for each feature |
| Can capture interactions | Not by judging features separately | Can, provided the model is able to capture them |
| Needs an importance measure from a model | No | Yes |
Contrast between judging features separately and judging them together
What do you think happens?
Must the model used for selection be the same kind of model you train for final predictions?
Reveal answer
No, the selection model only needs to provide an importance measure for each feature.
The selection model is a tool for judging features, and the final model is the one you deploy. You might rank features with a tree-based model and then train a different kind of model on the survivors.
Mistakes When Ranking Features
Common mistake Treating a signed coefficient as its importance directly
What it looks likeA coefficient of -1.50 is ranked below a coefficient of +0.20 because the number is smaller.
Why it failsThe sign gives direction, not strength. Ranking by raw signed values puts large negative effects at the bottom.
Do this insteadTake the absolute value before sorting, so the ranking reflects how large each effect is.
Common mistake Comparing coefficients across features on different scales
What it looks likeA feature measured in large units gets a big coefficient and is judged most important.
Why it failsThe coefficient size partly reflects the unit of measurement rather than the feature's real contribution.
Do this insteadCheck that features are on comparable scales, for example by standardizing them, before reading their coefficients as importance.
Common mistake Assuming decision tree importances need extra derivation
What it looks likeTrying to compute a separate score from the tree when the model already provides one.
Why it failsThe source states that these models directly encode importance through feature_importances_.
Do this insteadRead feature_importances_ from the fitted model and sort its positions.
Common mistake Assuming the transformer removes observations
What it looks likeExpecting the row count to shrink after applying SelectFromModel.
Why it failsThe transformer removes features, not observations.
Do this insteadCompare the shapes before and after; only the number of columns should change.
Common mistake Assuming univariate and model-based selection see the same thing
What it looks likeBelieving a feature that looks weak alone will never matter in combination with another.
Why it failsUnivariate methods judge features one at a time, while model-based selection considers all features together and can capture interactions if the model can.
Do this insteadChoose the approach based on whether you need joint judgment across features.
Practice: Ranking a Small Feature Set
A linear model fitted on standardized features gives coefficients of -0.80 for feature P, +0.35 for feature Q, and -0.10 for feature R. Using the absolute-value rule, rank the three features, then state which two you would keep if you retained the top two. Explain why the sign of P's coefficient does not change its rank.
Hints
- Start by taking the absolute value of each coefficient.
- Sort the absolute values from largest to smallest.
- The sign tells direction, which is set aside for ranking importance.
Key Takeaways
- Model-based feature selection fits a supervised model to the full feature set, obtains one importance measure per feature, ranks the features, and keeps the strongest ones.
- Decision tree-based models expose importance directly through feature_importances_, while linear models give importance through the absolute values of their coefficients, which works best on comparable scales.
SelectFromModelwraps the importance ranking as a transformer that outputs a reduced feature matrix with fewer columns and the same number of rows.- Univariate selection judges each feature alone, while model-based selection considers all features at once and can capture interactions if the model can.