6.1. Exercises

Regression Questions

Consider this toy dataset:

404 image

Question 1

404 image

Building a π‘˜-NN-Regressor

Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Let’s bring in this PokΓ©mon dataset again, but this time we are not going to be predicting a PokΓ©mon’s capture rate (capture_rt) instead of its legendary classification.

We did the same process of cross validation and scoring as we did before but we obtain this plot:

404 image

This model didn’t end up having a clear best score when we hyperparameter tuned but in the end, we decided to use n_neighbors=12.

Let’s build a KNeighborsRegressor with this hyperparameter value and see how well your model does on the test data.

Tasks:

  • Build a model using KNeighborsRegressor() using the optimal n_neighbors.
  • Save this in an object named model.
  • Fit your model on the objects X_train and y_train.
  • Evaluate the test score of the model using .score() on X_test and y_test and save the values in an object named test_score rounded to 4 decimal places.
Hint 1
  • Are using KNeighborsRegressor(n_neighbors=12)?
  • Are you using the model named model?
  • Are you calling .fit(X_train, y_train) on your model?
  • Are you scoring your model using model.score(X_test, y_test)?
  • Are you rounding to 4 decimal places?
  • Are you calculating test_score as round(model.score(X_test, y_test), 4)
Fully worked solution: