API Reference / features / selection /

variance.py

Variance-based feature selection.

This module provides feature selectors that remove low-variance features.

Classes

VarianceThresholdSelector

class features.selection.variance.VarianceThresholdSelector(FeatureSelector, SelectorMixin)

Feature selector that removes all low-variance features.

This is a simple baseline approach for feature selection. It removes all features whose variance doesn't meet a threshold. By default, it removes all zero-variance features, i.e., features that have the same value in all samples.
Constructor
__init__(
    self,
    threshold: float = 0.0,
)

Overview

The selector computes the variance of each feature across all samples. If the variance of a feature is less than or equal to the specified threshold, the feature is removed. This method is unsupervised as it does not look at the target values.

Theory

For each feature X_j, the variance is computed as:

\sigma^2(X_j) = \frac{1}{n} \sum_{i=1}^n (x_{ij} - \bar{x}_j)^2

If \sigma^2(X_j) \le \tau, where \tau is the threshold, feature j is discarded.

Parameters

threshold
float = 0.0
Features with a variance lower than or equal to this threshold will be removed. The default is to keep all features with non-zero variance.

Attributes

variances_
np.ndarray of shape (n_features,)
Variance of each feature computed from the training data.

Notes

When to use:
  • As a first step in feature selection to remove constant or near-constant features.
  • When you have a very large number of features and want a computationally
inexpensive way to reduce dimensionality.
  • In unsupervised scenarios where target labels are unavailable.
Limitations:
  • Does not take into account the relationship between features and the target.
  • Does not handle feature redundancy (redundant features with high variance
will both be kept).

Basic usage for removing zero-variance features:

python
>>> from tuiml.features.selection import VarianceThresholdSelector
>>> import numpy as np
>>> X = np.array([[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]])
>>> selector = VarianceThresholdSelector()
>>> X_new = selector.fit_transform(X)
>>> print(X_new)
[[2 0]
 [1 4]
 [1 1]]

Remove features with variance below 0.1:

python
>>> selector = VarianceThresholdSelector(threshold=0.1)
>>> X_new = selector.fit_transform(X)
>>> print(X_new)
[[2 0]
 [1 4]
 [1 1]]

Methods

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

Learn variances from X.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
Ignored
Not used, present for API consistency.
Returns
self
VarianceThresholdSelector
The fitted selector.
transform (self, X: np.ndarray) -> np.ndarray

Select features from X.

Parameters
X
ndarray of shape (n_samples, n_features)
Input data.
Returns
X_new
ndarray of shape (n_samples, n_selected_features)
Data with low-variance features removed.
get_support (self, indices: bool=False) -> np.ndarray

Get a mask or indices of selected features.

Parameters
indices
bool = False
If True, return indices; if False, return boolean mask.
Returns
support
ndarray
Boolean mask or indices of selected features.
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.