Sitemap

Data Preprocessing - Feature Scaling

2 min readMay 24, 2023

--

In this article, I will explain you Introduction to feature scaling and techniques available for feature scaling on data in machine learning.

Feature Scaling:

Feature scaling is a data preprocessing technique used in machine learning. It mainly scales the features or variables of our data to the similar range so that we can avoid domination of features with larger magnitude on those with smaller magnitude. It is one of the important techniques to make algorithms or models more stable, perform better and give best results.

Press enter or click to view image in full size

Techniques for Feature Scaling:

  • Scaling to a range (Min-Max): It basically scales the features of data to specific range that we want. But the default range is 0 to 1. Here, each record of feature is subtracted by the minimum value and divided by the range (maximum — minimum). This scaling is sensitive to outliers So, it is used when the feature is more-or-less uniformly distributed across a fixed range.

Get Leela Jagadeeswar P’s stories in your inbox

Join Medium for free to get updates from this writer.

from sklearn.preprocessing import MinMaxScaler
minmax_scaler = MinMaxScaler(feature_range = (0, 1))
X_scaled = minmax_scale.fit_transform(X)
  • Standardization (Z-score): It scales the features of data in such a way to have a mean of 0 and a standard deviation of 1. Here, each record is subtracted by the mean and divided by the standard deviation.
from sklearn.preprocessing import StandardScaler
standard_scaler = StandardScaler()
X_scaled = standard_scaler.fit_transform(X)
  • L1- L2 normalization: we can scale input vectors individually to unit norm (vector length) using L1 or L2 normalization. In L1 normalization, the sum of their absolute values is one. In L2 normalization, it is calculated as the square root of the sum of the squared vector values.
from sklearn.preprocessing import Normalizer
normalizer = Normalizer(norm = "l1")
X_scaled = normalizer.transform(X)
  • Log scaling: In Log scaling, it applies log to the values of feature so that the values are compress from a wide range to a narrow range. This scaling is preferred when there are extreme values means when data distribution is skewed.
import numpy as np
df["log_scaled" + col_name] = np.log(df[col_name])

We need to use the ‘inverse_transform’ function when we want to convert the transformed data i.e. mostly the model predictions back to its original scale which helps us in Evaluation & Output Presentation.

--

--