CommencerCommencer gratuitement

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

Afficher le cours

Instructions

  • Set n_neighbors in the KNeighborsRegressor to the best-performing value of 5 (found in the previous exercise).
  • Obtain predictions using the knn model from the scaled_train_features and scaled_test_features.
  • Create a scatter plot of the test_targets versus the test_predictions and label it test.

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()
Modifier et exécuter le code