Scaled vs Unscaled KNN: Compare Test Accuracy with a StandardScaler Pipeline

Quick Overview

Build a scikit-learn pipeline that standardizes features before k-nearest neighbors classification and compare its test accuracy with an unscaled model. Tests how feature scale affects distance-based models and how to fit preprocessing without leaking test data.

Scaled vs Unscaled KNN: Compare Test Accuracy with a StandardScaler Pipeline

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `build_scaled_knn(k=5)` so that it returns an unfitted scikit-learn `Pipeline` that standardizes features before KNN classification. Then implement `compare_scaling(X_train, y_train, X_test, y_test, k=5)`, which fits a plain `KNeighborsClassifier` and the scaled pipeline on the training data and returns both test accuracies. For example, suppose feature 0 lies in [0, 1] and separates the two classes, while feature 1 is noise drawn from a normal distribution with mean 0 and standard deviation 1000: ```python compare_scaling(X_train, y_train, X_test, y_test, k=5) # -> {"unscaled": ~0.5, "scaled": ~1.0} ``` ```hint Where the statistics come from Ask which rows the scaler is allowed to learn its mean and standard deviation from, and how the test rows get transformed with those same numbers. ``` ### Constraints & Assumptions - The pipeline steps are exactly `[("scaler", StandardScaler()), ("knn", KNeighborsClassifier(n_neighbors=k))]`. - The scaler learns its statistics from the training rows only (default `StandardScaler`, population standard deviation). - Both models use the default `KNeighborsClassifier` settings apart from `n_neighbors=k`. - Return `{"unscaled": float, "scaled": float}`, where accuracy is the fraction of test predictions that are correct. ### What a Strong Answer Covers - A pipeline with the exact step names and order, returned unfitted. - Both models fit on the same raw training data, with the scaler's mean and standard deviation taken from training rows only. - Test accuracies returned as Python floats under the two required keys. - Why the unscaled model falls to about chance on the example, in terms of how Euclidean distance combines features. ### Follow-up Questions - Why can't you call `StandardScaler().fit` on the full dataset before splitting? - When would `MinMaxScaler` or `RobustScaler` be a better choice than `StandardScaler`? - Can standardizing ever make KNN worse? Give a case. - How would you choose `k` for each model without touching the test set?

Overview: Build a scikit-learn pipeline that standardizes features before k-nearest neighbors classification and compare its test accuracy with an unscaled model. Tests how feature scale affects distance-based models and how to fit preprocessing without leaking test data.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement build_scaled_knn(k=5) so that it returns an unfitted scikit-learn Pipeline that standardizes features before KNN classification. Then implement compare_scaling(X_train, y_train, X_test, y_test, k=5), which fits a plain KNeighborsClassifier and the scaled pipeline on the training data and returns both test accuracies.

For example, suppose feature 0 lies in [0, 1] and separates the two classes, while feature 1 is noise drawn from a normal distribution with mean 0 and standard deviation 1000:

compare_scaling(X_train, y_train, X_test, y_test, k=5)
# -> {"unscaled": ~0.5, "scaled": ~1.0}

Constraints & Assumptions

  • The pipeline steps are exactly [("scaler", StandardScaler()), ("knn", KNeighborsClassifier(n_neighbors=k))] .
  • The scaler learns its statistics from the training rows only (default StandardScaler , population standard deviation).
  • Both models use the default KNeighborsClassifier settings apart from n_neighbors=k .
  • Return {"unscaled": float, "scaled": float} , where accuracy is the fraction of test predictions that are correct.

What a Strong Answer Covers Guidance

  • A pipeline with the exact step names and order, returned unfitted.
  • Both models fit on the same raw training data, with the scaler's mean and standard deviation taken from training rows only.
  • Test accuracies returned as Python floats under the two required keys.
  • Why the unscaled model falls to about chance on the example, in terms of how Euclidean distance combines features.

Follow-up Questions Guidance

  • Why can't you call StandardScaler().fit on the full dataset before splitting?
  • When would MinMaxScaler or RobustScaler be a better choice than StandardScaler ?
  • Can standardizing ever make KNN worse? Give a case.
  • How would you choose k for each model without touching the test set?