DEV Community

likhitha manikonda
likhitha manikonda

Posted on

Feature Scaling

🧑‍💻 Feature Scaling Made Simple

If you’re new to machine learning, you’ll often hear the term “feature scaling.” Don’t worry—it’s not as scary as it sounds! Let’s break it down step by step.


🌱 What is Feature Scaling?

  • Imagine you’re comparing the height of people (in centimeters) and their income (in dollars).
  • Heights might range from 150–200, while incomes could range from 10,000–100,000.
  • Because the numbers are on very different scales, some algorithms might think income is way more important than height—just because the values are bigger.

👉 Feature scaling is the process of bringing all features (columns of data) to a similar range so that no one feature dominates unfairly.


⚙️ Why Do We Need It?

  • Many machine learning algorithms (like K‑Nearest Neighbors, Gradient Descent, Neural Networks) calculate distances or gradients. If features are on different scales, results can be misleading.
  • Scaling makes training faster and improves accuracy.
  • It ensures fair comparison between features.

📏 Common Methods of Feature Scaling

Method How it Works When to Use
Min‑Max Normalization Rescales values to a fixed range, usually 0 to 1. Formula: ((x - min) / (max - min)) Good when you know the minimum and maximum values.
Standardization (Z‑Score) Centers data around 0 with a standard deviation of 1. Formula: ((x - mean) / std) Works well when data has outliers.
Robust Scaling Uses median and interquartile range instead of mean/std. Best when data has many extreme values.

🧩 Simple Example

Let’s say we have two features:

  • Height: [150, 160, 170, 180, 190]
  • Income: [20,000, 50,000, 100,000, 200,000, 500,000]

After Min‑Max Scaling (0–1):

  • Height → [0, 0.25, 0.5, 0.75, 1]
  • Income → [0, 0.06, 0.16, 0.36, 1]

Now both features are on the same scale, making them easier to compare.


📊 Feature Scaling in Python

Make sure you have scikit-learn installed:
pip install scikit-learn

import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler

# Example data: Height (cm) and Income ($)
data = np.array([
    [150, 20000],
    [160, 50000],
    [170, 100000],
    [180, 200000],
    [190, 500000]
])

print("Original Data:\n", data)

# 🔹 Min-Max Scaling (0–1 range)
minmax_scaler = MinMaxScaler()
data_minmax = minmax_scaler.fit_transform(data)
print("\nMin-Max Scaled Data:\n", data_minmax)

# 🔹 Standardization (mean=0, std=1)
standard_scaler = StandardScaler()
data_standard = standard_scaler.fit_transform(data)
print("\nStandardized Data:\n", data_standard)
Enter fullscreen mode Exit fullscreen mode

🧑‍💻 What this code does:

  • Creates a small dataset with Height and Income.
  • Applies Min‑Max Scaling → values between 0 and 1.
  • Applies Standardization → values centered around 0 with unit variance.
  • Prints results so you can see the difference

🚀 Key Takeaways

  • Feature scaling = putting features on similar ranges.
  • It prevents one feature from dominating just because of larger numbers.
  • Use Min‑Max for bounded data, Standardization for normal distributions, and Robust Scaling for data with outliers.

✨ Final Note

Think of feature scaling like adjusting the volume levels in a song. If one instrument is too loud, you won’t hear the others properly. Scaling balances the “volume” of your features so the algorithm hears them all clearly.


Top comments (0)