Feature Engineering: One-Hot Encoding, StandardScaler & MinMax0%

Feature Engineering: One-Hot Encoding, StandardScaler & MinMax

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Feature Engineering: One-Hot Encoding, StandardScaler & MinMax

Raw business data (text strings, timestamps, disparate numerical scales) cannot be ingested directly into mathematical estimators like Linear Regression, Logistic Regression, or Support Vector Machines. Feature Engineering & Preprocessing transforms raw columns into numerical representations while eliminating numerical scale distortion through feature scaling.


1. Encoding Categorical Variables

Algorithms require numeric matrices. Categorical strings must be converted systematically:

A. One-Hot Encoding (Nominal Categories)

Used for unordered categories (e.g., Department: ['Sales', 'HR', 'Engineering']).

  • Creates binary dummy indicator columns (0 or 1).
  • Dummy Variable Trap: When using linear models, set drop='first' to remove perfect collinearity!
Python
from sklearn.preprocessing import OneHotEncoder
import pandas as pd
 
encoder = OneHotEncoder(drop='first', sparse_output=False)
encoded_array = encoder.fit_transform(df[['department']])
encoded_df = pd.DataFrame(encoded_array, columns=encoder.get_feature_names_out())

B. Ordinal Encoding (Ordered Categories)

Used when categories have an inherent rank (e.g., ['Junior', 'Mid', 'Senior', 'Lead'] or ['Low', 'Medium', 'High']).

Python
from sklearn.preprocessing import OrdinalEncoder
 
edu_order = [['High School', 'Bachelors', 'Masters', 'PhD']]
ord_enc = OrdinalEncoder(categories=edu_order)
df['education_level'] = ord_enc.fit_transform(df[['education']])

2. Numerical Feature Scaling

When features have radically different scales (e.g., Age ranging from 18 to 70 vs Annual_Salary ranging from $30,000 to $300,000), distance-based algorithms (KNN, K-Means) and gradient descent algorithms (Logistic Regression, Neural Nets) will treat salary as 10,000 times more important than age!

Scaling Methods Comparison

ScalerFormulaResulting DistributionBest Used When
StandardScaler (Z-score)$z = rac{x - mu}{sigma}$Mean $mu = 0$, Std $sigma = 1$Data is Gaussian/normal; default for ML
MinMaxScaler$x_{norm} = rac{x - x_{min}}{x_{max} - x_{min}}$Bounded strictly between $[0, 1]$Neural nets, image pixels, non-Gaussian
RobustScaler$x_{rob} = rac{x - Q_2}{IQR}$Median 0, scaled by IQRDataset has severe uncleaned outliers

3. Implementing Scalers with Scikit-Learn

Python
from sklearn.preprocessing import StandardScaler, MinMaxScaler
 
# Initialize scaler
scaler = StandardScaler()
 
# Crucial rule: Fit ONLY on training data to prevent leakage!
X_train_scaled = scaler.fit_transform(X_train[['age', 'annual_salary']])
 
# Transform test data using fitted training parameters (do NOT refit!)
X_test_scaled = scaler.transform(X_test[['age', 'annual_salary']])
Cardinal Rule of Preprocessing: Never call fit() or fit_transform() on the entire dataset or test dataset! Scalers must learn parameters ($mu, sigma, x_{min}, x_{max}$) solely from X_train. Calling fit() on test data causes Data Leakage.

4. Tree-Based Algorithms Exception

Note
[!NOTE] Tree-based algorithms (Decision Trees, Random Forest, XGBoost, LightGBM) make splits based on monotonic feature ordering ($x > c$) and are completely invariant to monotonic scale. Scaling is not required for decision trees, but is mandatory for linear models, logistic regression, SVMs, and neural networks.

Interactive Quiz

Multiple Choice Questions

Question 1
Why must scaler.fit_transform() be called on X_train, but only scaler.transform() on X_test?
Question 2
Which scaling method transforms feature values to have a mean of 0 and a standard deviation of 1?
Question 3
Which family of machine learning algorithms is mathematically invariant to feature scaling and requires NO feature normalization?
Question 4
What is the Dummy Variable Trap in linear regression models when one-hot encoding categorical variables?
Question 5
If a dataset contains extreme outliers that cannot be immediately removed, which scaler is most resilient because it uses the median and Interquartile Range (IQR)?

Next Lesson

Train-Test Split, Cross-Validation & Avoiding Data Leakage

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext