Check our results

Once we have an optimized model, we want to check how it is performing in more detail. We already saw the R\(^2\) score, but it can be helpful to see the predictions plotted vs actual values. We can use the .predict() method of our decision tree model to get predictions on the train and test sets.

Ideally, we want to see diagonal lines from the lower left to the upper right. However, due to the simplicity of decisions trees, our model is not going to do well on the test set. But it will do well on the train set.

This exercise is part of the course

Machine Learning for Finance in Python

View Course

Exercise instructions

  • Create a DecisionTreeRegressor model called decision_tree using 3 for the max_depth hyperparameter.
  • Make predictions on the train and test sets (train_features and test_features) with our decision tree model.
  • Scatter the train and test predictions vs the actual target values with plt.scatter(), and set the label argument equal to test for the test set.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Use the best max_depth of 3 from last exercise to fit a decision tree
decision_tree = ____
decision_tree.fit(train_features, train_targets)

# Predict values for train and test
train_predictions = decision_tree.predict(train_features)
test_predictions = ____

# Scatter the predictions vs actual values
plt.scatter(train_predictions, train_targets, label='train')
plt.scatter(____)
plt.show()