Different Feature Selection Results Between Local Ubuntu VM and Databricks Using sklearn's SequentialFeatureSelector
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
In the realm of machine learning, feature selection is a critical step that can significantly affect the performance of a model. It involves selecting the most relevant features from your dataset, which can lead to improved accuracy, reduced overfitting, and decreased computational cost. This article delves into the differences observed when using `sklearn`'s `SequentialFeatureSelector` for feature selection in two different environments: a local Ubuntu VM setup and the Databricks platform.
Background
`SequentialFeatureSelector` is a part of the `sklearn` feature_selection module. It allows for both forward and backward feature selection, thereby enabling us to iteratively select or eliminate features based on a given performance metric. This technique is particularly useful in identifying the most advantageous predictor variables for a model.
Environment Setup
Local Ubuntu VM
- OS: Ubuntu 20.04 LTS
- Python: 3.8
- sklearn: 0.24.1
- Hardware: 8GB RAM, Dual-core Processor
Databricks Environment
- Runtime: Databricks Runtime 10.4 LTS
- Python: 3.8
- sklearn: 0.24.1
- Hardware: Standard DS3 v2 instance, 14GB RAM, 4 cores
Both environments used the same dataset and the same `SequentialFeatureSelector` configuration for a fair comparison.
Methodology
- Dataset: The "UCI Heart Disease" dataset was used for this study. It consists of 303 samples with 14 features.
- Model: A Logistic Regression model was employed as the base estimator.
- Feature Selector Configuration:
- `n_features_to_select=5`
- `direction='forward'`
- `scoring='accuracy'`
- `cv=5`
- Procedure:
- Fit the model to the dataset using `SequentialFeatureSelector` on both environments.
- Compare the selected features and execution time.
Results
Feature Selection
| Environment | Selected Features |
| Local Ubuntu VM | ['age', 'sex', 'cp', 'thalach', 'ca'] |
| Databricks | ['age', 'trestbps', 'chol', 'thalach', 'ca'] |
Execution Time
| Environment | Execution Time (seconds) |
| Local Ubuntu VM | 15.2 |
| Databricks | 10.4 |
Analysis
Discrepancies in Selected Features
The differences in selected features highlight the potential influence of various factors such as environment-specific optimizations, random initializations, and differences in parallel processing capabilities.
The Local Ubuntu VM selected 'sex' and 'cp' as important features, whereas Databricks selected 'trestbps' and 'chol'. Though both environments chose 'age', 'thalach', and 'ca', it indicates some consistency across both setups.
Performance Discrepancies
- Execution Time: Databricks outperformed the Local Ubuntu VM in terms of execution time due to its superior hardware specifications and potentially better optimized resource allocation.
- Consistency: Since `sklearn`'s `SequentialFeatureSelector` may use different conditions or leverage parallel processing differently across platforms, discrepancies in feature selection aren't unexpected.
Technical Considerations
Randomness in Feature Selection
Feature selection processes may inherently involve some randomness, especially with algorithms that have stochastic elements. This can lead to non-deterministic results across different runs and environments.
Impact of Parallel Processing
Databricks, having more resources at its disposal, can potentially handle parallel tasks more efficiently than a local setup, contributing to variations in execution time and possibly even in selected features.
Best Practices
- Multiple Runs: Conduct multiple runs and average the results to account for randomness.
- Environment Consistency: Ensure consistency in library versions and configurations across environments to minimize discrepancies.
- Resource Monitoring: Keep track of resource utilization to understand how it impacts feature selection outcomes.
Conclusion
Feature selection using `sklearn`'s `SequentialFeatureSelector` can yield different results based on the environment due to factors like hardware differences and underlying optimizations. Understanding these differences is crucial for building robust machine learning models. By staying aware of such disparities and adopting best practices, data scientists can make more informed decisions in their feature selection processes.
Related reading
- Different result with roc_auc_score and auc
- Different types of divisions in TensorFlow
- Differentiable round function in Tensorflow?
- Differnce between train_test_split and StratifiedShuffleSplit
- Digit Recognition on CNN
- Dimension of shape in conv1D
- Disable Tensorflow debugging information
- Disabling tf.function decorators for debugging?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.