Get startedGet started for free

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.

This exercise is part of the course

Machine Learning for Finance in Python

View Course

Exercise instructions

  • 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).

Hands-on interactive exercise

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

# Get predictions from model on train and test
train_predictions = rfr.predict(train_features)
test_predictions = ____

# Calculate and plot returns from our RF predictions and the SPY returns
test_returns = np.sum(returns_monthly.iloc[train_size:] * ____, axis=1)
plt.plot(test_returns, label='algo')
plt.plot(returns_monthly['SPY'].iloc[____], label='SPY')
plt.legend()
plt.show()
Edit and Run Code