Univariate feature selection methods.
This module provides univariate feature selectors that score each feature independently and select the best ones based on the scores.
-
SelectKBestSelector: keep the
khighest-scoring features. - SelectPercentileSelector: keep the top percentage of features.
- SelectThresholdSelector: keep every feature scoring above a threshold.
- SelectFprSelector: False positive rate threshold (p-value based)
Classes
class features.selection.univariate.SelectKBestSelector(FeatureSelector, SelectorMixin)
Select features according to the k highest scores.
__init__( self, score_func: Callable = None, k: Union[int, str] = 10, )
Overview
score_func. It is a fast filter method that provides a global ranking of feature importance based on statistical tests.Parameters
score_func
f_classif, chi2, or mutual_info_classif.
k
Attributes
scores_
pvalues_
score_func.
Notes
- To identify the most statistically significant individual features.
- As a baseline for more complex feature selection methods.
- When you need a fast and interpretable way to reduce feature count.
- Only captures univariate importance; ignores feature interactions.
- Does not handle redundant features (correlated features with high scores
See Also
Select top 5 features using information gain:
>>> from tuiml.features.selection import SelectKBestSelector
>>> from tuiml.evaluation.metrics import information_gain
>>> import numpy as np
>>> X, y = np.random.randn(20, 20), np.random.randint(0, 2, 20)
>>> selector = SelectKBestSelector(score_func=information_gain, k=5)
>>> X_new = selector.fit_transform(X, y)
>>> print(selector.get_support(indices=True))
Methods
class features.selection.univariate.SelectPercentileSelector(FeatureSelector, SelectorMixin)
Select features according to a percentile of the highest scores.
SelectKBestSelector that keeps a proportion of features rather than a fixed number.__init__( self, score_func: Callable = None, percentile: int = 10, )
Overview
Parameters
score_func
percentile
Attributes
scores_
pvalues_
Keep top 20% of features using Chi-Square test:
>>> from tuiml.features.selection import SelectPercentileSelector
>>> from tuiml.evaluation.metrics import chi2
>>> import numpy as np
>>> X, y = np.abs(np.random.randn(20, 50)), np.random.randint(0, 2, 20)
>>> selector = SelectPercentileSelector(score_func=chi2, percentile=20)
>>> X_new = selector.fit_transform(X, y)
>>> print(X_new.shape[1])
10
Methods
class features.selection.univariate.SelectFprSelector(FeatureSelector, SelectorMixin)
Select features based on false positive rate threshold.
alpha, thereby controlling the Probability of making a "False Positive" discovery.__init__( self, score_func: Callable = None, alpha: float = 0.05, )
Overview
score_func) to filter out features that are likely to be independent of the target variable.Parameters
score_func
alpha
Attributes
scores_
pvalues_
Notes
- To select only features that have a statistically significant relationship
- When you want to control the False Positive Rate (FPR) of the selection process.
Select features significant at 5% level:
>>> from tuiml.features.selection import SelectFprSelector
>>> from tuiml.evaluation.metrics import f_classif
>>> import numpy as np
>>> X, y = np.random.randn(50, 10), np.random.randint(0, 2, 50)
>>> selector = SelectFprSelector(score_func=f_classif, alpha=0.05)
>>> X_new = selector.fit_transform(X, y)
Methods
class features.selection.univariate.SelectThresholdSelector(FeatureSelector, SelectorMixin)
Select features based on score threshold.
__init__( self, score_func: Callable = None, threshold: float = 0.0, ignore_features: Optional[List[int]] = None, )
Overview
Parameters
score_func
threshold
ignore_features
Attributes
scores_
pvalues_
ranking_
Notes
SelectFprSelector, which thresholds p-values, this selector thresholds the raw score values (e.g., Information Gain bits).Select features with information gain >= 0.1:
>>> from tuiml.features.selection import SelectThresholdSelector
>>> from tuiml.evaluation.metrics import information_gain
>>> import numpy as np
>>> def mock_ig(X, y): return np.array([0.05, 0.15, 0.08, 0.12])
>>> X = np.random.randn(10, 4)
>>> y = np.random.randint(0, 2, 10)
>>> selector = SelectThresholdSelector(score_func=mock_ig, threshold=0.1)
>>> X_new = selector.fit_transform(X, y)
>>> print(selector.get_support(indices=True))
[1 3]