How to Create a Random Forest Classifier in Sklearn

02.25.2021

Intro

In this article, we will look at a Random Forest Classifier. The Random Forest model improves the tree model by training multiple tree models and select the best. This helps because a single tree model usually overfits. In this article, we will learn how to build a Random Forest Classifier using Sklearn.

Creating a RandomForestClassifier

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

from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets

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

randomforest = RandomForestClassifier()

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