Variance-based feature selection.
This module provides feature selectors that remove low-variance features.
Classes
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
- In unsupervised scenarios where target labels are unavailable.
- Does not take into account the relationship between features and the target.
- Does not handle feature redundancy (redundant features with high variance
See Also
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]]