Make predictions with a random forest
In order to fit a machine learning model to predict ideal portfolios, we need to create train and test sets for evaluating performance. We will do this as we did in previous chapters, where we take our features and targets arrays, and split them based on a train_size we set. Often the train size may be around 70-90% of our data.
We then fit our model (a random forest in this case) to the training data, and evaluate the R\(^2\) scores on train and test using .score() from our model. In this case, the hyperparameters have been set for you, but usually you'd want to do a search with ParameterGrid like we did in previous chapters.
Diese Übung ist Teil des Kurses
Machine Learning for Finance in Python
Anleitung zur Übung
- Set the
train_sizeto be 85% of the full training set data using the.shapeproperty offeatures. - Create train and test targets from
targetsusing Python indexing. - Fit the random forest model to the
train_featuresandtrain_targets.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Make train and test features
train_size = int(0.85 * ____)
train_features = features[:train_size]
test_features = features[train_size:]
train_targets = targets[____]
test_targets = targets[____]
# Fit the model and check scores on train and test
rfr = RandomForestRegressor(n_estimators=300, random_state=42)
rfr.fit(____, ____)
print(rfr.score(train_features, train_targets))
print(rfr.score(test_features, test_targets))