dataframe scaling
pandas
sklearn
data preprocessing
machine learning

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.

Practice ML system design

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: z=xμσz = \frac{x - \mu}{\sigma}zz: Standardized value • xx: Original value • μ\mu: Mean of the feature values • σ\sigma: Standard deviation of the feature values

Formulation: x=xxminxmaxxminx' = \frac{x - x_{\min}}{x_{\max} - x_{\min}}xx': Normalized value • xx: Original value • $x_\{\min\}$ and $x_\{\max\}$: Minimum and maximum values of the feature

Formulation: x=xmedian(X)IQR(X)x' = \frac{x - \text{median}(X)}{\text{IQR}(X)} • 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.