How to Create a Random Forest Regressor in Sklearn

02.26.2021

Intro

In a previous article, we learned how to build a random forest classifier. The regressor improved our tree model by training many tree models and selecting the best. Now, we will see how to do the same for regression. In this article, we will see how to create a Random Forest Regressor in Sklearn.

Creating a RandomForestRegressor

To create a RandomForestRegressor, we use the RandomForestRegressor class from the ensemble module. We create a instance of RandomForestRegressor then pass our data to the fit method as we usually do when building models.

from sklearn.ensemble import RandomForestRegressor
from sklearn import datasets

iris = datasets.load_iris()
features = iris.data
target = iris.target

randomforest = RandomForestRegressor()

model = randomforest.fit(features, target)
print(model.score())