pandas dataframe columns scaling with sklearn
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 machine learning, feature scaling is a crucial step in the preprocessing phase. Data often consist of features that vary widely in range and units. Consequently, this can lead to poor model performance as many algorithms are sensitive to the ranges of input data. Pandas and Scikit-learn (`sklearn`) are powerful libraries in Python commonly used for data preprocessing and machine learning tasks. In this article, we will explore different techniques for scaling pandas DataFrame columns using `sklearn`.
Why Scale Data?
Scaling is vital for algorithms like gradient descent-based methods, K-nearest neighbors (KNN), principal component analysis (PCA), and support vector machines (SVMs), where data variance directly impacts performance. By scaling, we ensure that:
• Standardization: Features are centered around zero with a standard deviation of one. • Normalization: Features are scaled to have a minimum value of zero and a maximum value of one. • Robustness: Feature scaling reduces the influence of outliers on the model performance.
Techniques for Scaling
The three primary techniques for scaling data using `sklearn` are Standardization, Min-Max Scaling, and Robust Scaling. Each method caters to different scaling requirements.
1. Standardization
Standardization scales the data by removing the mean and scaling to unit variance. It assumes the data follows a Gaussian distribution and is also known as Z-score normalization.
• Formulation: • : Standardized value • : Original value • : Mean of the feature values • : Standard deviation of the feature values
• Formulation:
• : Normalized value
• : Original value
• $x_\{\min\}$ and $x_\{\max\}$: Minimum and maximum values of the feature
• Formulation: • IQR: Interquartile Range, the difference between 75th and 25th percentile
• Choosing the Right Scaler: The choice of scaling method depends on data distribution and the algorithm you plan to use. Min-Max scaling is often used in image processing, while standardization is commonly used in algorithms assuming Gaussian distribution. • Preserving Transformation: Ensure the same scaler object is applied to both training and testing datasets to preserve the transformations. • Categorical Data: Scaling is intended for numerical data. Categorical data need to be transformed into numerical formats (e.g., one-hot encoding) before applying scaling.
Related reading
- Parallel fitting of multiple Keras Models on single GPU
- Parallel jobs don't finish in scikit-learn's GridSearchCV
- Parallel processes in distributed tensorflow
- Parallel threads with TensorFlow Dataset API and flat_map
- Pandas dataframe fillna only some columns in place
- Pandas dataframe get first row of each group
- Parallelization strategies for deep learning
- Parameter Tuning for Perceptron Learning Algorithm
.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 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.