1. Learn
  2. /
  3. Courses
  4. /
  5. Machine Learning for Finance in Python

Connected

Exercise

Get predictions and first evaluation

Now that we have a trained random forest model (rfr), we want to use it to get predictions on the test set. We do this to evaluate our model's performance – at a basic level, is it doing as well or better than just buying the index, SPY?

We'll use the typical sklearn .predict(features) method, then multiply our monthly returns by our portfolio predictions. We sum these up with np.sum() since this will have 3 rows for each month. Then we plot both the monthly returns from our predictions, as well as SPY and compare the two.

Instructions

100 XP
  • Use the rfr random forest model's .predict() method to make predictions on train_features and test_features.
  • Multiply the test set portion of returns_monthly by test_predictions to get the returns of our test set predictions.
  • Plot the test set returns_monthly for 'SPY' (everything from train_size to the end of the data).