CodeBlanks Logo Icon CodeBlanks

What is scaling?

Scaling brings feature values to a specific range.

What is scaling?

Features can come in different units: age 20-60, salary 10,000-90,000. Scaling moves them all into a comparable range so none dominates just because of its unit.

a = np.array([30, 40000])
b = np.array([60, 41000])
print(np.linalg.norm(a - b))   # ~1000.4  the age gap (30) is almost invisible

Scaling doesn't change the shape of the data; it only stretches or squeezes the axes.

Which models are affected?

Affected: models using distances or gradients (KNN, SVM, K-means, PCA, regularised linear models, neural networks). Not affected: tree-based models (decision tree, random forest, gradient boosting).

# affected: KNN, SVC, LogisticRegression(C=...), Ridge, Lasso, KMeans, PCA, MLP
# not affected: DecisionTree, RandomForest, GradientBoosting

In regularisation (Ridge/Lasso) the penalty is applied equally to all weights; if scales differ, the penalty is distributed unfairly.

Practice See the dominance

Compute the distance between a and b, round it to 1 digit and print it. Then divide salary by 1000 (thousand TL), recompute the same distance and print it.

Starter code

model.py
import numpy as np

a = np.array([30.0, 40000.0])
b = np.array([60.0, 41000.0])

# buraya yaz

Sample solution

model.py
import numpy as np

a = np.array([30.0, 40000.0])
b = np.array([60.0, 41000.0])

print(round(np.linalg.norm(a - b), 1))
olcek = np.array([1, 1000])
print(round(np.linalg.norm(a / olcek - b / olcek), 1))

Expected output

1000.4
30.0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 In unscaled data, what mostly determines the distance between two examples?

  • The most important feature
  • The first column
  • The label
  • The feature with the widest range Correct answer

Explanation: Since squared differences are summed, the large-valued feature crushes the total.

2 Compute the vector length.

Question
d = np.linalg.____(a - b)
Answer
d = np.linalg.norm(a - b)

Explanation: np.linalg.norm gives the Euclidean length by default.

3 Match the models with their scale sensitivity.

Term Meaning
KNN Uses distance, affected
RandomForest Uses thresholds, unaffected
Ridge Penalty becomes unfair
PCA Looks at variance, affected

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation