Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

SelectFromModel

SelectFromModel is a meta-transformer that keeps features whose model-derived importance meets a threshold. It can fit the wrapped estimator itself or consume a prefit estimator.

Since
0.17

How it works

SelectFromModel wraps an estimator and uses its learned coefficients or feature importances to build a feature mask. During fitting it can train an unfitted estimator, or it can use an already fitted one with prefit=True; transform then keeps features whose absolute importance reaches the threshold, optionally limited by max_features.

Syntax

class sklearn.feature_selection.SelectFromModel(estimator, *, threshold=None, prefit=False, norm_order=1, max_features=None, importance_getter='auto')

Parameters

NameTypeDescription
estimatorobjectBase fitted or unfitted estimator used to obtain feature importance values.
thresholdstr or float, default=NoneSelection cutoff; numeric values and supported heuristic strings are accepted.
prefitbool, default=FalseWhether the supplied estimator is already fitted.
norm_orderint, default=1Norm order used to aggregate coefficients for multi-class estimators.
max_featuresint, callable, default=NoneMaximum number of features to select; an integer or callable.
importance_getterstr or callable, default='auto'Attribute path or callable used to obtain importance values.

Returns

A fitted SelectFromModel transformer that can transform data using the selected-feature mask.

Key points

  • It is a transformer that uses model-derived importance values to reduce the feature set.
  • Selection compares each feature’s absolute importance with threshold and retains values meeting or exceeding it.
  • importance_getter='auto' reads coefficients or feature_importances_ from the wrapped estimator.
  • The class has been available since scikit-learn 0.17.

Common mistakes

  • With prefit=True, call transform directly; cloning-based model-selection utilities cannot use the prefit estimator.
  • If max_features alone should determine selection, explicitly set threshold=-np.inf.
  • The wrapped estimator must expose coefficients or feature importances unless importance_getter supplies another source.

Example: Select features from a fitted model

from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import LogisticRegression

X, y = load_iris(return_X_y=True)
estimator = LogisticRegression(penalty="l1", solver="liblinear", max_iter=1000)
selector = SelectFromModel(estimator)
X_selected = selector.fit_transform(X, y)
print(X.shape, X_selected.shape)

Fits the estimator, derives feature importances from its coefficients, and returns only features meeting the default threshold.

When to use it

Model-based dimensionality reduction

Reduce dimensionality before a downstream classifier or regressor.

Sparse linear-model selection

Use sparse coefficients from an L1-penalized linear model to remove zero-weight features.

Importance thresholding with a feature cap

Keep only the strongest model-ranked variables, optionally with a hard count limit.

Interview questions

QWhat does SelectFromModel do?

It is a meta-transformer that selects features using an estimator’s importance weights. After fitting, it removes features whose absolute importance is below the configured threshold.

Follow-up: How can it support an estimator without a direct coef_ or feature_importances_ attribute? Set importance_getter to a suitable attribute path or callable that extracts the importances.

QHow does threshold control selection?

By default, threshold=None uses a built-in threshold heuristic, while threshold can also be a number or strings such as mean, median, or a multiple of them. A feature is retained when its absolute importance is at least the threshold.

Follow-up: How would you cap the selected feature count? Supply max_features; to make that cap the only criterion, use threshold=-np.inf.

QWhat is the difference between fitted and prefit usage?

With prefit=False, SelectFromModel fits the estimator and then transforms the data. With prefit=True, the estimator is expected to be fitted already, so transform must be called directly.

Follow-up: Why is prefit=True unsuitable for GridSearchCV? Such utilities clone estimators, which conflicts with passing an already fitted model for direct transformation.

  • sklearn.feature_selection.SelectPercentile
  • sklearn.feature_selection.RFE
  • sklearn.feature_selection.SequentialFeatureSelector
  • sklearn.feature_selection.VarianceThreshold

Source: SelectFromModel — scikit-learn 1.9.1 documentation. Checked 2026-10-15.