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.
pip install numpy scikit-learn
Python code
24 linesimport 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
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
- Use `SelectorKBest` with an ANOVA F-value metric to select features based on correlation with the target.
- 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
More from ML engineering pipelines
- Bayesian Optimization in Python: A Simplified Mock Implementation medium
- Build a Data Helper Class in Python for ML Pipelines easy
- Build a Mock Random Forest Classifier in Python easy
- Champion Challenger Deployment Mock in Python easy
- Compare Model A vs Model B Metrics in Python easy
- Create a Minimal Great Expectations Suite Mock in Python easy
Keep learning
Related tutorials and quizzes for this topic.