How to do feature selection with VarianceThreshold in Python

This code demonstrates how to use scikit-learn's VarianceThreshold to remove low-variance features from a NumPy array, keeping only those that vary enough to be useful for modeling.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 14 views 0 copies

Requires third-party packages — install first
pip install numpy scikit-learn

Python code

24 lines
Python 3.9+
import numpy as np
from sklearn.feature_selection import VarianceThreshold

def main():
    # Mock dataset: 4 samples, 5 features
    X = np.array([
        [0.1, 0.2, 1.0, 1.0, 0.5],
        [0.2, 0.2, 0.0, 1.0, 0.4],
        [0.1, 0.2, 1.0, 1.0, 0.6],
        [0.3, 0.2, 1.0, 0.0, 0.5]
    ])

    # Select features with variance above threshold 0.1
    selector = VarianceThreshold(threshold=0.1)
    X_selected = selector.fit_transform(X)

    print("Original features shape:", X.shape)
    print("Selected features shape:", X_selected.shape)
    print("Selected features (columns):", selector.get_support(indices=True))
    print("Transformed data:")
    print(X_selected)

if __name__ == "__main__":
    main()

Output

stdout
Original features shape: (4, 5)
Selected features shape: (4, 3)
Selected features (columns): [0 2 3]
Transformed data:
[[0.1 1.  1. ]
 [0.2 0.  1. ]
 [0.1 1.  1. ]
 [0.3 1.  0. ]]

How it works

VarianceThreshold computes the variance of each feature column and keeps only those whose variance exceeds the threshold (default 0.0). Features with near-zero variance carry little information and can hurt model performance. The fit_transform method both learns the column mask and applies it to the input array. get_support(indices=True) returns the integer indices of the retained columns. This is a simple, unsupervised filter that works well for numeric data with similar scales, but you should standardize features first if scales differ.

Common mistakes

  • Using `threshold=0.1` without understanding that variances must be comparable; features on different scales may be incorrectly dropped.
  • Forgetting to standardize features (e.g., with `StandardScaler`) before variance filtering, leading to scale-dependent results.
  • Calling `fit_transform` on the whole dataset including the target column, accidentally dropping the target or leaking information.

Variations

  1. Use `SelectorKBest` with an ANOVA F-value metric to select features based on correlation with the target.
  2. Apply `VarianceThreshold` inside a scikit-learn `Pipeline` to integrate it into a full model workflow.

Real-world use cases

  • In a machine learning pipeline, removing constant or near-constant columns from sensor data before training a classifier.
  • Reducing the dimensionality of high-cardinality one-hot encoded features in a marketing response model to avoid noise.
  • As a preprocessing step in a data processing script to automatically drop redundant columns from a large CSV dump.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from ML engineering pipelines

Related tutorials and quizzes for this topic.