Scaling and dimensionality
Quick answer
Distance-based models can be dominated by features measured on large numerical scales. Scaling changes the geometry of the neighborhood and must be learned from training data only. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
See a change in nearest row
q=[0,0]rows=[[1,90],[3,0]]for scale in [[1,1],[1,100]]: print([round(sum(((a-b)/s)**2 for a,b,s in zip(r,q,scale)),2) for r in rows])Expected output:
[8101.0, 9.0][1.81, 9.0]Raw distance favors row 1; rescaling the second coordinate favors row 0. Scales here are illustrative, not fitted.
How it works
Suppose two features measure a small vibration score and a temperature in tenths of a degree. A change of 100 in the second coordinate overwhelms a change of 2 in the first under raw Euclidean distance. Standardization maps feature j to (x_j - mean_j)/scale_j, with training mean and population standard deviation for StandardScaler. A constant training column gets scale 1 in this implementation, avoiding division by zero; a varying future value still contributes distance. Scaling does not guarantee equal predictive relevance. Domain-specific units, robust scaling or learned feature selection may be better. For pairwise distances, subtracting the same center cancels; dividing each coordinate difference by its scale changes relative influence. Fit the scaler inside a pipeline so that each validation fold uses its own training statistics. In high dimensions, irrelevant coordinates add distance noise and samples may become sparse relative to the volume; relative nearest/farthest contrast can shrink under many common distributions, not as a universal mathematical law. If a unit hypercube is sampled uniformly, a subcube of side 1/2 occupies fraction (1/2)^d of its volume. With ten dimensions that is 1/1024. Dimensionality reduction or feature selection should itself be trained inside the validation procedure. Sparse text often benefits from cosine geometry rather than centering large sparse matrices.
Fit training-only scales
from sklearn.preprocessing import StandardScalers=StandardScaler().fit([[0,10],[2,10]])print(s.mean_.tolist(),s.scale_.tolist())print(s.transform([[4,12]]).tolist())Expected output:
[1.0, 10.0] [1.0, 1.0][[3.0, 2.0]]The held-out row does not change the fitted means or scales.
Common mistakes
Never fit a scaler before splitting data. Do not divide by a zero standard deviation or assume scaling removes irrelevant features. Do not present high-dimensional distance concentration as inevitable for every dataset.
Practice and next steps
Compare the fraction occupied by a half-side subcube in 2 and 10 dimensions. Explain why adding noise features can hurt KNN. Complete the knowledge check and coding exercise. Next: Gaussian naive Bayes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Scaling and dimensionality — knowledge check
Apply scaling and dimensionality to new examples.
Why scale before KNN distance calculation?
When is the scaler fitted?
What fraction of a unit cube is a half-side subcube in dimension d?
What does a constant StandardScaler training column use as scale?
Does standardization guarantee relevant features?