API Reference / features / selection /

univariate.py

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 k highest-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

SelectKBestSelector

class features.selection.univariate.SelectKBestSelector(FeatureSelector, SelectorMixin)

Select features according to the k highest scores.

Univariate feature selection: computes a score for each feature and selects the top k performing features.
Constructor
__init__(
    self,
    score_func: Callable = None,
    k: Union[int, str] = 10,
)

Overview

This selector evaluates each feature independently using a provided score_func. It is a fast filter method that provides a global ranking of feature importance based on statistical tests.

Parameters

score_func
callable = f_classif
Function taking (X, y) and returning (scores, pvalues) or scores. Usually a statistical test like f_classif, chi2, or mutual_info_classif.
k
int or "all" = 10
Number of top features to select. If "all", no features are removed.

Attributes

scores_
np.ndarray of shape (n_features,)
Individual feature scores.
pvalues_
np.ndarray of shape (n_features,) or None
P-values of feature scores, if supported by score_func.

Notes

When to use:
  • 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.
Limitations:
  • Only captures univariate importance; ignores feature interactions.
  • Does not handle redundant features (correlated features with high scores
will all be selected).

Select top 5 features using information gain:

python
>>> 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

fit (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'SelectKBestSelector'

Fit the selector to the data.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
ndarray of shape (n_samples,)
Target values.
Returns
self
SelectKBestSelector
The fitted selector.
transform (self, X: np.ndarray) -> np.ndarray

Transform X by selecting the k highest-scoring features.

Parameters
X
ndarray of shape (n_samples, n_features)
Input data.
Returns
X_new
ndarray of shape (n_samples, n_selected_features)
Data with selected features.
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.

SelectPercentileSelector

class features.selection.univariate.SelectPercentileSelector(FeatureSelector, SelectorMixin)

Select features according to a percentile of the highest scores.

A variant of SelectKBestSelector that keeps a proportion of features rather than a fixed number.
Constructor
__init__(
    self,
    score_func: Callable = None,
    percentile: int = 10,
)

Overview

This selector is useful when you want to reduce dimensionality while keeping a relative instead of absolute number of features across different datasets.

Parameters

score_func
callable = f_classif
Function taking (X, y) and returning (scores, pvalues) or scores.
percentile
int = 10
Percent of features to keep (between 0 and 100).

Attributes

scores_
np.ndarray of shape (n_features,)
Individual feature scores.
pvalues_
np.ndarray of shape (n_features,) or None
P-values of feature scores.

Keep top 20% of features using Chi-Square test:

python
>>> 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

fit (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'SelectPercentileSelector'

Fit the selector to the data.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
ndarray of shape (n_samples,)
Target values.
Returns
self
SelectPercentileSelector
The fitted selector.
transform (self, X: np.ndarray) -> np.ndarray

Transform X by selecting the top percentile of features.

Parameters
X
ndarray of shape (n_samples, n_features)
Input data.
Returns
X_new
ndarray of shape (n_samples, n_selected_features)
Data with selected features.
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.

SelectFprSelector

class features.selection.univariate.SelectFprSelector(FeatureSelector, SelectorMixin)

Select features based on false positive rate threshold.

Keeps features whose p-values are below a significance level alpha, thereby controlling the Probability of making a "False Positive" discovery.
Constructor
__init__(
    self,
    score_func: Callable = None,
    alpha: float = 0.05,
)

Overview

This selector uses a statistical significance test (provided by score_func) to filter out features that are likely to be independent of the target variable.

Parameters

score_func
callable = f_classif
Function taking (X, y) and returning (scores, pvalues). Must return p-values for thresholding.
alpha
float = 0.05
Maximum p-value threshold for keeping a feature.

Attributes

scores_
np.ndarray of shape (n_features,)
Individual feature scores.
pvalues_
np.ndarray of shape (n_features,)
P-values associated with each feature.

Notes

When to use:
  • To select only features that have a statistically significant relationship
with the target.
  • When you want to control the False Positive Rate (FPR) of the selection process.

Select features significant at 5% level:

python
>>> 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

fit (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'SelectFprSelector'

Fit the selector to the data.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
ndarray of shape (n_samples,)
Target values.
Returns
self
SelectFprSelector
The fitted selector.
transform (self, X: np.ndarray) -> np.ndarray

Transform X by selecting features with p-values below alpha.

Parameters
X
ndarray of shape (n_samples, n_features)
Input data.
Returns
X_new
ndarray of shape (n_samples, n_selected_features)
Data with only statistically significant features.
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.

SelectThresholdSelector

class features.selection.univariate.SelectThresholdSelector(FeatureSelector, SelectorMixin)

Select features based on score threshold.

Keeps features with univariate scores above (or equal to) a specified threshold.
Constructor
__init__(
    self,
    score_func: Callable = None,
    threshold: float = 0.0,
    ignore_features: Optional[List[int]] = None,
)

Overview

This selector filters features by their raw score value rather than rank or p-value. It also allows ignoring specific feature indices during the ranking process.

Parameters

score_func
callable = f_classif
Function taking (X, y) and returning (scores, pvalues) or scores.
threshold
float = 0.0
Minimum score threshold. Features with scores >= threshold are retained.
ignore_features
list of int
Indices of features to ignore during ranking. These features will never be selected, regardless of their score.

Attributes

scores_
np.ndarray of shape (n_features,)
Individual feature scores.
pvalues_
np.ndarray of shape (n_features,) or None
P-values of feature scores, if available.
ranking_
np.ndarray of shape (n_features,)
Feature indices sorted by score in descending order.

Notes

Comparing to FPR: Unlike SelectFprSelector, which thresholds p-values, this selector thresholds the raw score values (e.g., Information Gain bits).

Select features with information gain >= 0.1:

python
>>> 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]

Methods

fit (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'SelectThresholdSelector'

Fit the selector to the data.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
ndarray of shape (n_samples,)
Target values.
Returns
self
SelectThresholdSelector
The fitted selector.
transform (self, X: np.ndarray) -> np.ndarray

Transform X by selecting features with scores above the threshold.

Parameters
X
ndarray of shape (n_samples, n_features)
Input data.
Returns
X_new
ndarray of shape (n_samples, n_selected_features)
Data with only the features meeting the score threshold.
get_ranked_features (self) -> np.ndarray

Get features sorted by score (highest to lowest).

Returns
ranking
ndarray of shape (n_features, 2)
Array with columns [feature_index, score], sorted by score descending.
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.