Back
+20 XP
Concept

Data Normalization: Min-Max Scaling & Z-Score

Disparate feature scales corrupt distance-based algorithms. Min-Max compresses values to $[0, 1]$ via $\frac{x - x_{min}}{x_{max} - x_{min}}$. Z-Score standardizes to mean $0$ and variance $1$ via $\frac{x - \mu}{\sigma}$ ⚖️.

python
import math

def min_max(data):
    low, high = min(data), max(data)
    return [(x - low) / (high - low) for x in data] if high > low else [0.0] * len(data)

def z_score(data):
    mean = sum(data) / len(data)
    std = math.sqrt(sum((x - mean) ** 2 for x in data) / len(data))
    return [(x - mean) / std for x in data] if std > 0 else [0.0] * len(data)

nums = [10, 20, 30, 40, 50]
print(f"Min-Max: {min_max(nums)} ⚖️")
print(f"Z-Score: {[round(z, 2) for z in z_score(nums)]} 📉")

# 58. Data Normalization: Min-Max Scaling & Z-Score Disparate feature scales corrupt distance-based algorithms. Min-Max compresses values to $[0, 1]$ via $\frac{x - x_{min}}{x_{max} - x_{min}}$. Z-Score standardizes to mean $0$ and variance $1$ via $\frac{x - \mu}{\sigma}$ ⚖️. ```python import math def min_max(data): low, high = min(data), max(data) return [(x - low) / (high - low) for x in data] if high > low else [0.0] * len(data) def z_score(data): mean = sum(data) / len(data) std = math.sqrt(sum((x - mean) ** 2 for x in data) / len(data)) return [(x - mean) / std for x in data] if std > 0 else [0.0] * len(data) nums = [10, 20, 30, 40, 50] print(f"Min-Max: {min_max(nums)} ⚖️") print(f"Z-Score: {[round(z, 2) for z in z_score(nums)]} 📉") ``` Always compute transformation parameters ($\mu, \sigma, min, max$) exclusively on your training split, then apply those frozen statistics to the test set to avoid catastrophic data leakage 🛡️! --- ### Question Why is Z-score standardization preferred over Min-Max scaling when a dataset contains extreme outliers? 🧠 - [ ] a. Min-Max compresses normal points into a tiny sub-range because extreme values anchor the min/max endpoints ⚖️ - [ ] b. Z-score standardization replaces floating-point math with ultra-fast integer bit-shifts - [ ] c. Min-Max scaling is non-deterministic and produces inconsistent results across repeated runs - [ ] d. Z-score mathematically forces all outputs to be strictly bounded within [0.0, 1.0] --- ### Challenge Implement `min_max(data)`. Scale the list `[10, 20, 30, 40, 50]` to $[0, 1]$. Print the resulting list of floats. 🎯 <details> <summary>Solution</summary> ```python def min_max(data): low, high = min(data), max(data) return [(x - low) / (high - low) for x in data] if high > low else [0.0] * len(data) nums = [10, 20, 30, 40, 50] print(min_max(nums)) ``` </details>

Checkpoint

Why is Z-score standardization preferred over Min-Max scaling when a dataset contains extreme outliers? 🧠

Practice

Implement `min_max(data)`. Scale the list `[10, 20, 30, 40, 50]` to $[0, 1]$. Print the resulting list of floats. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks