/Curriculum/Practical & ProjectsMachine Learning

47. NumPy with Machine LearningAdvanced

Min-Max scaling, Z-score standardization, One-Hot Encoding, and Euclidean distance.

18 mins

Concept Overview

Feature scaling (Min-Max Normalization and Z-score Standardization) is essential in Machine Learning to ensure all features contribute equally to gradient descent.

Hardware Mental Model

Standardization is putting apples, elephants, and rocket ships on the exact same zero-centered scale (mean 0, variance 1) so algorithms can compare them fairly.

Key Concepts (1)Click snippet to load in editor

Z-score formula: $Z = \frac{X - \mu}{\sigma}$. Min-Max formula: $X_{norm} = \frac{X - X_{min}}{X_{max} - X_{min}}$.

X_norm = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:mean = X.mean() # Computes 1 global mean across all different feature columns!
✓ Correct:mean = X.mean(axis=0) # Computes separate mean per feature column

Feature scaling must always use `axis=0` to scale each column feature independently.

Pro Tip: Always specify `axis=0` when normalizing ML feature matrices.

📌 Quick Revision

Core takeaway points from this topic
Z-score Standardization: centers data at mean=0 with standard deviation=1.
Min-Max Normalization: compresses values into $[0.0, 1.0]$ range.
Vectorized Euclidean Distance: `np.linalg.norm(a - b)`.
One-Hot Encoding: `np.eye(num_classes)[labels]`.
Editor: 47. NumPy with Machine Learning
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Compute Euclidean Distance

Given `p1 = np.array([0, 0])` and `p2 = np.array([3, 4])`, compute Euclidean distance `np.linalg.norm(p1 - p2)` and print.

Expected Output Target:5.0
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state