Measure performance
Now that we've fit our neural net, let's check performance to see how well our model is predicting new values. There's not a built-in .score() method like with sklearn models, so we'll use the r2_score() function from sklearn.metrics. This calculates the R\(^2\) score given arguments (y_true, y_predicted). We'll also plot our predictions versus actual values again. This will yield some interesting results soon (once we implement our own custom loss function).
Questo esercizio fa parte del corso
Machine Learning for Finance in Python
Istruzioni dell'esercizio
- Obtain predictions from
model_1on the scaled test set data (scaled_test_featuresandtest_targets). - Print the R\(^2\) score on the test set (
test_targetsandtest_preds). - Plot the
test_predsversustest_targetsin a scatter plot withplt.scatter().
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
from sklearn.metrics import r2_score
# Calculate R^2 score
train_preds = model_1.predict(scaled_train_features)
test_preds = model_1.predict(____)
print(r2_score(train_targets, train_preds))
print(r2_score(____, ____))
# Plot predictions vs actual
plt.scatter(train_preds, train_targets, label='train')
plt.scatter(____)
plt.legend()
plt.show()