Feature Selection in PySpark
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Feature selection in PySpark reduces the number of input variables to improve model performance and reduce training time on large datasets. PySpark's MLlib provides VectorSlicer for index-based selection, ChiSqSelector for statistical selection, and UnivariateFeatureSelector (Spark 3.1+) for flexible statistical tests. For correlation-based filtering and variance thresholds, you combine PySpark DataFrame operations with these selectors.
VectorSlicer (Index-Based Selection)
VectorSlicer selects features by their index positions within a feature vector:
Use VectorSlicer when you already know which feature indices to keep (e.g., from prior analysis).
ChiSqSelector (Statistical Selection)
ChiSqSelector uses the chi-squared test to select features most correlated with the label (for classification tasks):
Selection Types
UnivariateFeatureSelector (Spark 3.1+)
UnivariateFeatureSelector supports multiple statistical tests depending on feature and label types:
| Feature Type | Label Type | Test Used |
| continuous | categorical | ANOVA F-test |
| categorical | categorical | Chi-squared |
| continuous | continuous | F-regression |
Correlation-Based Filtering
Remove highly correlated features to reduce redundancy:
Variance Threshold
Remove features with near-zero variance (they carry no information):
Feature Selection in a Pipeline
Common Pitfalls
- ChiSqSelector on continuous labels: The chi-squared test is designed for categorical labels. Using it with continuous targets gives meaningless results. Use
UnivariateFeatureSelectorwithfeatureType="continuous"andlabelType="continuous"for regression tasks. - Feature indices change after VectorSlicer: After slicing, feature indices reset to 0, 1, 2, ... in the output vector. If you need to map back to original feature names, maintain a mapping from the assembler's
inputCols. - Not assembling features first: PySpark MLlib selectors work on vector columns, not individual DataFrame columns. You must use
VectorAssemblerto combine individual columns into a feature vector before applying any selector. - Correlation matrix on sparse data:
Correlation.corr()converts the feature vector to a dense matrix. For datasets with millions of features, this causes out-of-memory errors. Use sampling or batch processing for very wide datasets. - Applying feature selection after train-test split: Fit the selector on training data only and use
model.transform()on test data. Fitting on the full dataset leaks information from the test set into the feature selection process.
Summary
- Use
VectorSlicerfor manual index-based feature selection - Use
ChiSqSelectorfor statistical feature selection in classification tasks - Use
UnivariateFeatureSelector(Spark 3.1+) for flexible statistical tests across feature and label types - Compute the correlation matrix with
Correlation.corr()and drop redundant features above a threshold - Always assemble individual columns into a vector with
VectorAssemblerbefore applying selectors - Integrate feature selection into a
Pipelinefor reproducible workflows
Related reading
- Feature selection using bigram
- Feature selection using scikit-learn
- Feature/Variable importance after a PCA analysis
- feed data into a tf.contrib.data.Dataset like a queue
- Fetch all rows in cassandra
- FileNotFound Exception when trying to store file in hadoop distributed cache
- Feedforward Algorithm in NEAT Neural Evolution of Augmenting Topologies
- Feeding data through an embedding wrapper in TensorFlow

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.