CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Machine Learning for Finance in Python

Afficher le cours

Instructions

  • Set the train_size to be 85% of the full training set data using the .shape property of features.
  • Create train and test targets from targets using Python indexing.
  • Fit the random forest model to the train_features and train_targets.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# 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))
Modifier et exécuter le code