Model-based dimensionality reduction
Reduce dimensionality before a downstream classifier or regressor.
Scikit-Learn · Class
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.
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.
class sklearn.feature_selection.SelectFromModel(estimator, *, threshold=None, prefit=False, norm_order=1, max_features=None, importance_getter='auto')| Name | Type | Description |
|---|---|---|
estimator | object | Base fitted or unfitted estimator used to obtain feature importance values. |
threshold | str or float, default=None | Selection cutoff; numeric values and supported heuristic strings are accepted. |
prefit | bool, default=False | Whether the supplied estimator is already fitted. |
norm_order | int, default=1 | Norm order used to aggregate coefficients for multi-class estimators. |
max_features | int, callable, default=None | Maximum number of features to select; an integer or callable. |
importance_getter | str or callable, default='auto' | Attribute path or callable used to obtain importance values. |
A fitted SelectFromModel transformer that can transform data using the selected-feature mask.
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.
Reduce dimensionality before a downstream classifier or regressor.
Use sparse coefficients from an L1-penalized linear model to remove zero-weight features.
Keep only the strongest model-ranked variables, optionally with a hard count limit.
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.
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.
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.SelectPercentilesklearn.feature_selection.RFEsklearn.feature_selection.SequentialFeatureSelectorsklearn.feature_selection.VarianceThreshold