5.1. Exercises

Regression with Decision Tree True or False

Building a Decision Tree 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 build a decision tree regressor using DecisionTreeRegressor() and letโ€™s set some different hyperparameters.

Tasks:

  • Build a model using DecisionTreeRegressor() and make sure to set the argument random_state to 1.
  • Set the max_depth of the tree to 8.
  • Save your model in an object named reg_tree.
  • Fit your model on the objects X and y and then predict on X.
  • Save the R^2 score of the model rounded to 2 decimal places in a variable named tree_score.
Hint 1
  • Are using DecisionTreeRegressor(random_state=1, max_depth=8)?
  • Are you using the model named reg_tree?
  • Are you calling .fit(X,y) on your model?
  • Are you using .score(X,y) and rounding to 2 decimal places by using round()?
Fully worked solution: