Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
X_train = np.array([[60, 40500], [27, 46000], [30, 90000], [65, 80000]])
y_train = np.array([0, 1, 1, 0])
x = np.array([[25, 40000]])
raw = KNeighborsClassifier(n_neighbors=1).fit(X_train, y_train)
scaler = StandardScaler().fit(X_train)
scaled = KNeighborsClassifier(n_neighbors=1).fit(scaler.transform(X_train), y_train)
print(raw.predict(x)[0], scaled.predict(scaler.transform(x))[0])0 1
- Expecting
0 0: scaling changes the distances, so after it the age gap of 35 years outweighs the income gap of 6000, and the nearest point becomes[27, 46000], class1. - Expecting
1without scaling: raw income differences are thousands and age differences are tens, so income alone picks[60, 40500], class0.
- Why must the scaler be fitted on the training set only and then applied to the query point?
- Would a decision tree's prediction change after the same scaling, and why?