LoslegenKostenlos loslegen

Fit a random forest

Data scientists often use random forest models. They perform well out of the box, and have lots of settings to optimize performance. Random forests can be used for classification or regression; we'll use it for regression to predict the future price change of LNG.

We'll create and fit the random forest model similarly to the decision trees using the .fit(features, targets) method. With sklearn's RandomForestRegressor, there's a built-in .score() method we can use to evaluate performance. This takes arguments (features, targets), and returns the R\(^2\) score (the coefficient of determination).

Diese Übung ist Teil des Kurses

Machine Learning for Finance in Python

Kurs anzeigen

Anleitung zur Übung

  • Create the random forest model with the imported RandomForestRegressor class.
  • Fit (train) the random forest using train_features and train_targets.
  • Print out the R\(^2\) score on the train and test sets.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

from sklearn.ensemble import RandomForestRegressor

# Create the random forest model and fit to the training data
rfr = ____(n_estimators=200)
rfr.fit(____, ____)

# Look at the R^2 scores on train and test
print(rfr.score(train_features, train_targets))
print(____)
Code bearbeiten und ausführen