A nearest-neighbors model decides like this: to predict a new customer, it finds the training customers whose numbers are closest to theirs, and gives back the answer most of them had. KNeighborsClassifier(n_neighbors=5) makes one that asks the 5 closest. Its fit only stores the training rows and their answers; the searching happens when it predicts or scores. score gives back the share of test customers it gets right.
model = KNeighborsClassifier(n_neighbors=3)
model.fit([[1, 20], [2, 25], [9, 300], [10, 280]], [0, 0, 1, 1])
print(model.predict([[8, 290]])) # [1]: 2 of its 3 nearest had 1
Closest means the smallest distance between two rows of numbers, like on a map. That makes the units matter: spent runs in the hundreds while returned_item is 0 or 1, so in raw numbers the dollars decide almost alone who is near. Scaled features, like the StandardScaler ones from the scaling lesson, give every feature a fair say. The starting code already makes X_train_scaled and X_test_scaled.
The number of neighbors, k, matters too. With k = 1, one odd customer decides alone; with more neighbors, they vote. Your loop tries k = 1, 5 and 15, on raw and on scaled rows, and must print this. The last two lines come from the lesson's check, which runs after your code:
k = 1 raw: 0.627 scaled: 0.64 k = 5 raw: 0.653 scaled: 0.707 k = 15 raw: 0.72 scaled: 0.747 The first test customer's 5 nearest neighbors, bought_again: [1 1 0 1 1] So k = 5 predicts: 1
Your task: loop over k in [1, 5, 15]. In each round, make a KNeighborsClassifier with k neighbors, fit and score it on the raw rows, then fit it again on the scaled rows and print k with both scores.
Assignment
Follow the Coding Tutorial to compare nearest-neighbors models with 1, 5 and 15 neighbors, on raw and on scaled features.
Hint
A model fit on scaled rows must be scored on X_test_scaled, not X_test. Keep the raw score in raw before the second fit, and indent every line inside the loop by 4 spaces.
Free plan: 30 tutor messages. PRO is unlimited.
