DATA PREPROCESSING
Encoding and Feature scaling
What is Encoding ?
Encoding is the process of converting categorical data into a numerical format so that Machine Learning algorithms can understand and process it.
For example:
GenderMale
Female
Male
Femalecan be converted into:
1
0
1
0However, we should not use the same encoding technique for every categorical variable. The appropriate technique depends on the type of categorical data.
Types of Categorical Data
Before choosing an encoding technique, it is important to understand the two major types of categorical data.
1. Nominal Data
Nominal data consists of categories that do not have a natural order or ranking.
Examples:
Color:
Red
Blue
GreenThere is no meaningful relationship such as:
Red < Blue < GreenOther examples include:
- Gender
- City
- Country
- Blood group
- Product category
- Department
For nominal data, One-Hot Encoding is commonly used.
2. Ordinal Data
Ordinal data consists of categories that have a meaningful order or ranking.
For example:
Education:School
Graduate
Postgraduate
PhDThere is a natural progression between the categories.
Another example:
Customer Satisfaction:Low
Medium
HighHere, the order is meaningful:
Low < Medium < HighFor ordinal data, Ordinal Encoding is generally suitable.
Major Encoding Techniques
There are several techniques used to convert categorical variables into numerical values.
The commonly used techniques are:
- Label Encoding
- Ordinal Encoding
- One-Hot Encoding
- Binary Encoding
1. Label Encoding
1. Standardization
Standardization transforms numerical data so that the feature has approximately:
- Mean = 0
- Standard Deviation = 1
The formula is : [ z = \frac{x-\mu}{\sigma} ]
Where:
x= original valueμ= mean of the featureσ= standard deviationz= standardized value
After standardization, the values are centered around zero.
Python Implementation
from sklearn.preprocessing import StandardScalerscaler = StandardScaler()X_scaled = scaler.fit_transform(X)
Example
Suppose:
Age20
25
30
35
40After Standard Scaler, the values will be transformed into values centered around zero.
They may look approximately like:
-1.41
-0.71
0.00
0.71
1.41The exact values depend on the mean and standard deviation of the data.
When Should You Use Standard Scaler?
Standardization is a good general-purpose choice for many algorithms, including:
- Logistic Regression
- Linear Regression
- SVM
- KNN
- PCA
- Neural Networks
2. Min-Max Scaling
Min-Max Scaling transforms the values into a specified range.
The most common range is:
0 to 1The formula is : [ x’ = \frac{x-x_{min}}{x_{max}-x_{min}} ]
Where:
x= original valuexmin= minimum valuexmax= maximum valuex'= scaled value
Example
Suppose we have:
Age20
30
40
50
60After Min-Max Scaling:
20 → 0.00
30 → 0.25
40 → 0.50
50 → 0.75
60 → 1.00Python Implementation
from sklearn.preprocessing import MinMaxScalerscaler = MinMaxScaler()X_scaled = scaler.fit_transform(X)
When Should You Use MinMax Scaler?
Min-Max Scaling can be useful when:
- You want features within a fixed range.
- Your algorithm benefits from bounded values.
- You are working with neural networks or distance-based algorithms.
Important Limitation
Min-Max Scaling is sensitive to outliers.
Suppose:
Salary30,000
40,000
50,000
60,000
10,000,000The extremely large value can significantly affect the minimum-to-maximum range.
3. Robust Scaling
Robust Scaling is designed to reduce the influence of outliers.
Instead of using:
- Mean
- Standard deviation
Robust Scaling uses:
- Median
- Interquartile Range (IQR)
The formula is : [ x’ = \frac{x-\text{Median}}{IQR} ]
where : [IQR = Q3-Q1]
Python Implementation
from sklearn.preprocessing import RobustScalerscaler = RobustScaler()X_scaled = scaler.fit_transform(X)
Example
Consider:
Salary30,000
40,000
45,000
50,000
55,000
5,000,000Here, 5,000,000 is an extreme value.
Standard Scaler can be strongly influenced by this outlier.
Robust Scaler uses the median and IQR, making it more resistant to extreme observations.
When Should You Use Robust Scaler?
Use Robust Scaler when:
- Your numerical features contain significant outliers.
- You do not want extreme values to strongly influence the scaling parameters.
4. Max Absolute Scaling
Max Absolute Scaling divides each feature by its maximum absolute value.
It generally scales the values into:
-1 to 1while preserving the sign of the original values.
Python Implementation
from sklearn.preprocessing import MaxAbsScalerscaler = MaxAbsScaler()X_scaled = scaler.fit_transform(X)
When Should You Use MaxAbs Scaler?
It can be useful when:
- Data contains positive and negative values.
- You want to preserve sparsity.
- You are working with sparse datasets.
5. Normalization
Normalization is different from the feature-wise scaling methods discussed above.
Feature scaling methods such as Standard Scaler generally transform each feature/column.
Normalization typically transforms each sample/row so that its norm equals a specified value.
For example:
from sklearn.preprocessing import Normalizernormalizer = Normalizer()X_normalized = normalizer.fit_transform(X)
Common normalization methods include:
- L1 normalization
- L2 normalization
- Max normalization
Normalization is particularly useful in areas such as:
- Text classification
- Document similarity
- Cosine similarity
- Sparse dataconverts each category into a unique integer.
For example:
GenderMale
Female
Male
FemaleAfter Label Encoding:
Male → 1
Female → 0Another example:
CityHyderabad → 0
Mumbai → 1
Delhi → 2Python Example
from sklearn.preprocessing import LabelEncoderencoder = LabelEncoder()df["Gender"] = encoder.fit_transform(df["Gender"])
When to use?
Label Encoding can be useful when:
- The variable has two categories.
- The numerical representation will not create a problematic artificial relationship.
- You specifically need integer labels.
2. Ordinal Encoding
Ordinal Encoding is used when categories have a natural order.
Get Nea’s stories in your inbox
Join Medium for free to get updates from this writer.
For example:
SizeSmall
Medium
LargeWe can encode them as:
Small → 0
Medium → 1
Large → 2The numerical values preserve the order.
Python Example
from sklearn.preprocessing import OrdinalEncoderencoder = OrdinalEncoder(
categories=[["Small", "Medium", "Large"]]
)df["Size"] = encoder.fit_transform(df[["Size"]])
Another example:
Low → 0
Medium → 1
High → 2When to use?
Use Ordinal Encoding when:
- Categories have a meaningful order.
- The distance/order represented by the encoding makes sense for the model.
3. One-Hot Encoding
One-Hot Encoding is one of the most commonly used techniques for nominal categorical variables.
Suppose we have:
ColorRed
Blue
GreenOne-Hot Encoding creates a separate column for each category.
ColorRedBlueGreenRed100Blue010Green001
Each row contains 1 for the category it belongs to and 0 for the others.
Python Example
from sklearn.preprocessing import OneHotEncoderencoder = OneHotEncoder(
handle_unknown="ignore"
)encoded_data = encoder.fit_transform(
df[["Color"]]
)
When to use?
One-Hot Encoding is commonly used for nominal variables such as:
- City
- Gender
- Country
- Product type
- Department
- Browser type
4. Binary Encoding
Binary Encoding converts categories into binary numbers.
For example, instead of creating one column for every category, categories are first assigned integer values and then converted into binary representation.
Suppose:
CategoryA → 1
B → 2
C → 3
D → 4Their binary representations can be used as encoded features.
Binary Encoding can produce fewer columns than One-Hot Encoding when there are many categories.
When to use?
Binary Encoding can be useful for:
- High-cardinality categorical variables
- Large datasets
- Situations where One-Hot Encoding would create too many columns
- It is less commonly used than One-Hot Encoding in basic Machine Learning workflows.
Feature Scaling
Feature Scaling in Machine Learning
After encoding categorical variables, the next important preprocessing technique is Feature Scaling.
What is Feature Scaling?
Feature scaling is the process of transforming numerical features so that they are represented on a similar scale or range.
Types of Feature Scaling
The major feature scaling techniques are:
- Standardization
- Min-Max Scaling
- Robust Scaling
- Max Absolute Scaling
- Normalization
1. Standardization
Standardization transforms numerical data so that the feature has approximately:
- Mean = 0
- Standard Deviation = 1
The formula is:
[
z = \frac{x-\mu}{\sigma}
]
Where:
x= original valueμ= mean of the featureσ= standard deviationz= standardized value
After standardization, the values are centered around zero.
Python Implementation
from sklearn.preprocessing import StandardScalerscaler = StandardScaler()X_scaled = scaler.fit_transform(X)
Example
Suppose:
Age20
25
30
35
40After Standard Scaler, the values will be transformed into values centered around zero.
They may look approximately like:
-1.41
-0.71
0.00
0.71
1.41The exact values depend on the mean and standard deviation of the data.
When Should You Use StandardScaler?
Standardization is a good general-purpose choice for many algorithms, including:
- Logistic Regression
- Linear Regression
- SVM
- KNN
- PCA
- Neural Networks
2. Min-Max Scaling
Min-Max Scaling transforms the values into a specified range.
The most common range is:
0 to 1The formula is:
[
x’ = \frac{x-x_{min}}{x_{max}-x_{min}}
]
Where:
x= original valuexmin= minimum valuexmax= maximum valuex'= scaled value
Example
Suppose we have:
Age20
30
40
50
60After Min-Max Scaling:
20 → 0.00
30 → 0.25
40 → 0.50
50 → 0.75
60 → 1.00Python Implementation
from sklearn.preprocessing import MinMaxScalerscaler = MinMaxScaler()X_scaled = scaler.fit_transform(X)
When Should You Use MinMax Scaler?
Min-Max Scaling can be useful when:
- You want features within a fixed range.
- Your algorithm benefits from bounded values.
- You are working with neural networks or distance-based algorithms.
Important Limitation
Min-Max Scaling is sensitive to outliers.
Suppose:
Salary30,000
40,000
50,000
60,000
10,000,000The extremely large value can significantly affect the minimum-to-maximum range.
3. Robust Scaling
Robust Scaling is designed to reduce the influence of outliers. Instead of using:
- Mean
- Standard deviation
Robust Scaling uses:
- Median
- Interquartile Range (IQR)
The formula is : [ x’ = \frac{x-\text{Median}}{IQR} ]
where : [ IQR = Q3-Q1 ]
Python Implementation
from sklearn.preprocessing import RobustScalerscaler = RobustScaler()X_scaled = scaler.fit_transform(X)
Example
Consider:
Salary30,000
40,000
45,000
50,000
55,000
5,000,000Here, 5,000,000 is an extreme value.
Standard Scaler can be strongly influenced by this outlier.
Robust Scaler uses the median and IQR, making it more resistant to extreme observations.
When Should You Use Robust Scaler?
Use Robust Scaler when:
- Your numerical features contain significant outliers.
- You do not want extreme values to strongly influence the scaling parameters.
4. Max Absolute Scaling
Max Absolute Scaling divides each feature by its maximum absolute value.
It generally scales the values into:
-1 to 1while preserving the sign of the original values.
Python Implementation
from sklearn.preprocessing import MaxAbsScalerscaler = MaxAbsScaler()X_scaled = scaler.fit_transform(X)
When Should You Use MaxAbsScaler?
It can be useful when:
- Data contains positive and negative values.
- You want to preserve sparsity.
- You are working with sparse datasets.
5. Normalization
Normalization is different from the feature-wise scaling methods discussed above.
Feature scaling methods such as StandardScaler generally transform each feature/column.
Normalization typically transforms each sample/row so that its norm equals a specified value.
For example:
from sklearn.preprocessing import Normalizernormalizer = Normalizer()X_normalized = normalizer.fit_transform(X)
Common normalization methods include:
- L1 normalization
- L2 normalization
- Max normalization
Normalization is particularly useful in areas such as:
- Text classification
- Document similarity
- Cosine similarity
- Sparse data




