Implementation:Scikit learn Scikit learn SelectorMixin
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Feature Selection |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for providing a generic feature selection mixin with transform and inverse_transform functionality, provided by scikit-learn.
Description
The SelectorMixin class is a transformer mixin that performs feature selection given a support mask. It provides get_support, transform, inverse_transform, and get_feature_names_out methods. Subclasses must implement _get_support_mask to define which features to select.
Usage
Use this mixin as a base class when implementing custom feature selectors. It provides the standard scikit-learn transformer interface for feature selection, handling both dense and sparse input data.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/feature_selection/_base.py
Signature
class SelectorMixin(TransformerMixin, metaclass=ABCMeta):
def get_support(self, indices=False):
def transform(self, X):
def inverse_transform(self, X):
def get_feature_names_out(self, input_features=None):
@abstractmethod
def _get_support_mask(self):
Import
from sklearn.feature_selection import SelectorMixin
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | array-like of shape (n_samples, n_features) | Yes | Input data to transform or inverse-transform |
| indices | bool | No | If True, get_support returns integer indices instead of boolean mask (default False) |
Outputs
| Name | Type | Description |
|---|---|---|
| support | ndarray | Boolean mask or integer indices of selected features |
| X_transformed | ndarray of shape (n_samples, n_selected_features) | Reduced feature matrix |
| X_original | ndarray of shape (n_samples, n_features) | Feature matrix with zeros for non-selected features |
Usage Examples
Basic Usage
import numpy as np
from sklearn.datasets import load_iris
from sklearn.base import BaseEstimator
from sklearn.feature_selection import SelectorMixin
class FeatureSelector(SelectorMixin, BaseEstimator):
def fit(self, X, y=None):
self.n_features_in_ = X.shape[1]
return self
def _get_support_mask(self):
mask = np.zeros(self.n_features_in_, dtype=bool)
mask[:2] = True # select the first two features
return mask
X, y = load_iris(return_X_y=True)
print(FeatureSelector().fit_transform(X, y).shape) # (150, 2)