Evaluate our results
Once we have our linear fit and predictions, we want to see how good the predictions are so we can decide if our model is any good or not. Ideally, we want to back-test any type of trading strategy. However, this is a complex and typically time-consuming experience.
A quicker way to understand the performance of our model is looking at regression evaluation metrics like R\(^2\), and plotting the predictions versus the actual values of the targets. Perfect predictions would form a straight, diagonal line in such a plot, making it easy for us to eyeball how our predictions are doing in different regions of price changes. We can use matplotlib
's .scatter()
function to create scatter plots of the predictions and actual values.
This exercise is part of the course
Machine Learning for Finance in Python
Exercise instructions
- Show
test_predictions
vstest_targets
in a scatterplot, with 20% opacity for the points (use thealpha
parameter to set opacity). - Plot the perfect prediction line using
np.arange()
and the minimum and maximum values from the xaxis (xmin
,xmax
). - Display the legend on the plot with
plt.legend()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Scatter the predictions vs the targets with 20% opacity
plt.scatter(train_predictions, train_targets, alpha=0.2, color='b', label='train')
plt.scatter(____, ____, ____, color='r', label='test')
# Plot the perfect prediction line
xmin, xmax = plt.xlim()
plt.plot(np.arange(xmin, xmax, 0.01), np.arange(____, ____, 0.01), c='k')
# Set the axis labels and show the plot
plt.xlabel('predictions')
plt.ylabel('actual')
____ # show the legend
plt.show()