Using kNN in scikit-learn: Score and Update k
00:00 In this lesson, you’ll continue coding in scikit-learn to score and update the hyperparameter k of your kNN model.
In a previous lesson, you created
y data from a dataset about abalones.
X contains the physical measurements of the abalones, while
y stores their rings, which you tried to predict using kNN.
You went on to split your data into training and test sets. You used
sklearn to create a kNN model that makes predictions based on the three closest neighbors.
00:36 You fit that model to your training data and then made predictions with it for the features of both the training and test sets. Now, let’s determine how good those predictions are by scoring them against the actual values.
00:51 There are many different scoring methods to choose from, but for this lesson, let’s use mean squared error. You can use scikit-learn’s implementation by first importing it.
Then calculate the error for your training set by applying
pred_train. So you just need to use the two rays that you’d like to compare as the arguments here.
And let’s go ahead and save that output as
mse_train. Taking a look at
mse_train, you see that you have an error of just over 2.7, but what does that value actually mean?
MSE can be difficult to understand at first glance because it reports error in squared units. Let’s switch over to RMSE, or root mean squared error. So
rmse_train will be equal to the square root of
mse. You can just use NumPy’s square root function to calculate it.
np, which is for NumPy,
sqrt(), which is a square root function, and then we’ll take the square root of
rmse_train is about 1.65, which is in the same units as your original target.
02:18 Your kNN model predictions are off by about 1.65 rings for the abalones in your training set.
02:27 But for a more realistic result, you should see how your model performs on data that it hasn’t actually ever seen, and that’s the test set. So let’s find the mean squared error of the actual target values in your test set compared to the predictions of your kNN model.
Let’s call that
mse_test and that will be the
pred_test. Once again, let’s go ahead and take the square root of this.
rsme_test will be the square root of
rmse for the test set is about 2.38. So if you used this model to predict values with new abalone physical measurements, you can expect it to be off by about 2.38 rings.
So far, you’ve used a value of
3 for your kNN model, which means the algorithm only considers the three nearest neighbors when making a prediction.
The k is a so-called hyperparameter of kNN, and it needs to be adjusted to an appropriate value each time you apply kNN to a new dataset. Imagine you set
k equal to
1, so you’d only consider the closest neighbor when making a prediction.
03:48 Your predictions probably wouldn’t be very good because they would vary a lot from one point to another. That’s called high variance. However, if you set k to be very high, say the size of the entire dataset, you might be using neighbors that are very far away to make predictions, and you’d lose out on the nuances of your dataset. That’s called high bias.
04:12 Instead, the ideal k will be somewhere in the middle, and you’ll use some kind of validation strategy in order to find it.
04:22 Your model currently has an RMSE for the test dataset of about 2.38, but let’s try a different value for k to see if this helps lower the RMSE for your test set.
Create a new kNN model called
knn_model_25, and that’s also going to be a
KNeighborsRegressor, except for now we’re going to set
k equal to
n_neighbors=25. That means that when your model’s making predictions, it will look at the twenty-five closest neighbors in order to make that prediction.
Now you’re going to train that model with your training data using the
.fit() method, so
knn_model2_25.fit(), and you’re fitting this to
And make predictions for your test set. Let’s call those
pred_test_25, and those will be equal to your
Okay, so the predictions have been made, and you can go ahead and score these against the actual rings of your test abalones. Set
mse_test_25 equal to the
mean_squared_error() of your
y_test and your
05:49 And just like before, you can compute the RMSE of your test set, which is the square root of your your MSE.
Let’s take a look at
rmse_test_25, and you can see that this value has now decreased from 2.38 to 2.17. By considering twenty-five neighbors when making a prediction, your kNN model has less variability from point to point. That makes your test error lower and means that your kNN generalizes better to unseen data.
The choice of 25 neighbors was somewhat arbitrary in this lesson, but in actuality, you would likely use a validation set or a cross-validation method like
.GridSearchCV() to select the best hyperparameter k for your particular situation. While this process is outside the scope of this lesson, you can learn more about validation or cross-validation elsewhere on the Real Python platform.
06:56 You’ve now completed building a kNN model in Python’s scikit-learn. Coming up next, you’ll conclude this course by reviewing all you’ve learned about kNN, including its primary attributes, the main steps of the algorithm, and the code you used to make kNN predictions in Python.
Become a Member to join the conversation.