Evaluate KNN performance
We just saw a few things with our KNN scores. For one, the training scores started high and decreased with increasing n, which is typical. The test set performance reached a peak at 5 though, and we will use that as our setting in the final KNN model.
As we have done a few times now, we will check our performance visually. This helps us see how well the model is predicting on different regions of actual values. We will get predictions from our knn model using the .predict() method on our scaled features. Then we'll use matplotlib's plt.scatter() to create a scatter plot of actual versus predicted values.
Cet exercice fait partie du cours
Machine Learning for Finance in Python
Instructions
- Set
n_neighborsin theKNeighborsRegressorto the best-performing value of 5 (found in the previous exercise). - Obtain predictions using the
knnmodel from thescaled_train_featuresandscaled_test_features. - Create a scatter plot of the
test_targetsversus thetest_predictionsand label ittest.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Create the model with the best-performing n_neighbors of 5
knn = KNeighborsRegressor(____)
# Fit the model
knn.fit(scaled_train_features, train_targets)
# Get predictions for train and test sets
train_predictions = ____
test_predictions = ____
# Plot the actual vs predicted values
plt.scatter(train_predictions, train_targets, label='train')
plt.scatter(____)
plt.legend()
plt.show()