ComeçarComece de graça

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.

Este exercício faz parte do curso

Machine Learning for Finance in Python

Ver curso

Instruções do exercício

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

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# 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()
Editar e executar o código