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.
Latihan ini merupakan bagian dari kursus
Machine Learning for Finance in Python
Instruksi latihan
- Use the
rfrrandom forest model's.predict()method to make predictions ontrain_featuresandtest_features. - Multiply the test set portion of
returns_monthlybytest_predictionsto get the returns of our test set predictions. - Plot the test set
returns_monthlyfor'SPY'(everything fromtrain_sizeto the end of the data).
Latihan interaktif langsung praktik
Cobalah latihan ini dengan melengkapi kode contoh ini.
# 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()