Skip to content

Random Forest Classification

RandomForestClassifier can be used to tackle classification problems with machine learning and it is known to be  a very accurate classification model and perform very well out of the box in most cases.

In this tutorial we will look at RandomForestClassifier and how it can be used in Python with Scikit-learn library.

How to Construct?

RandomForestClassifier

You can import DecisionTreeClassifier from sklearn.tree module as below and use it to create a Decision Tree model object.

Creating RandomForestClassifier Model:

from sklearn.tree import RandomForestClassifier
RF = RandomForestClassifier()

Once the model is created next steps will be to fit the model and it will be ready for prediction.

Training RandomForestClassifier Model:

Once the model is created next steps will be to fit the model and in this phase model is being trained with training data.

RF.fit(X_train, y_train)

Predicting with RandomForestClassifier Model:

After training model will be ready for predictions.

yhat = RF.predict(X_test)

DecisionTreeClassifier has plenty of hyperparameters that can be tuned. Tuning the model can be used to:

How to make Random Forest models instantly faster without losing any accuracy?

Native Parallelization

Random Forest implementation in Scikit-learn has a useful parameter named n_jobs which can be used to take advantage of parallel computation.

n_jobs is 1 by default which means if you don’t define it specifically Random Forest model will use only one processor at a time. You can simply set it to -1 which will employ all processors in a computer during the training and prediction phases of the model.

Here is a simple implementation in Python:

RF = RandomForestRegressor(n_jobs = -1)

Random Forest models are packed with useful hyperparameters that can be tuned for improved accuracy, and efficiency. You can read more about tuning random forests here: 

RandomForestClassification Summary

This concludes our Random Forest Classification tutorial. In this tutorial we have seen how to creat a Random Forest Classifier model, train it and then make predictions with it.