Machine Learning Basics: Random Forest Regression | by Gurucharan M K | Towards Data Science

Previously, I had explained the various Regression models such as Linear, Polynomial, Support Vector and Decision Tree Regression. In this article, we will go through the code for the application of Random Forest Regression which is an extension to the Decision Tree Regression implemented previously.
The Decision Tree is an easily understood and interpreted algorithm and hence a single tree may not be enough for the model to learn the features from it. On the other hand, Random Forest is also a “Tree”-based algorithm that uses the qualities features of multiple Decision Trees for making decisions.
Therefore, it can be referred to as a ‘Forest’ of trees and hence the name “Random Forest”. The term ‘Random’ is due to the fact that this algorithm is a forest of ‘Randomly created Decision Trees’.
The Decision Tree algorithm has a major disadvantage in that it causes over-fitting. This problem can be limited by implementing the Random Forest Regression in place of the Decision Tree Regression. Additionally, the Random Forest algorithm is also very fast and robust than other regression models.
To summarize in short, The Random Forest Algorithm merges the output of multiple Decision Trees to generate the final output.
The same data set that was used for the Decision Tree Regression is utilized in this where we have one independent variable Temperature and one independent variable Revenue which we have to predict. In this problem, we have to build a Random Forest Regression Model which will study the correlation between the Temperature and Revenue of the Ice Cream Shop and predict the revenue for the ice cream shop based on the temperature on a particular day.
As usual, the NumPy, matplotlib and the Pandas libraries are imported.
import numpy as npimport matplotlib.pyplot as pltimport pandas as pd
The data set is imported using the function ‘pd.read_csv’ from my github repository. In this, we assign the independent variable (X) to the ‘Temperature’ column and the dependent variable (y) to the ‘Revenue’ column.
dataset = pd.read_csv('https://raw.githubusercontent.com/mk-gurucharan/Regression/master/IceCreamData.csv')X = dataset['Temperature'].valuesy = dataset['Revenue'].valuesdataset.head(5)>>Temperature   Revenue24.566884     534.79902826.005191     625.19012227.790554     660.63228920.595335     487.70696011.503498     316.240194
Similar to the Decision Tree Regression Model, we will split the data set, we use test_size=0.05 which means that 5% of 500 data rows (25 rows) will only be used as test set and the remaining 475 rows will be used as training set for building the Random Forest Regression Model.
from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.05)
In this step, to train the model, we import the RandomForestRegressor class and assign it to the variable regressor. We then use the .fit() function to fit the X_train and y_train values to the regressor by reshaping it accordingly.
# Fitting Random Forest Regression to the datasetfrom sklearn.ensemble import RandomForestRegressorregressor = RandomForestRegressor(n_estimators = 10, random_state = 0)regressor.fit(X_train.reshape(-1,1), y_train.reshape(-1,1))
In this step, we predict the results of the test set with the model trained on the training set values using the regressor.predict function and assign it to ‘y_pred’.
y_pred = regressor.predict(X_test.reshape(-1,1))
In this step, we shall compare and display the values of y_test as ‘Real Values’ and y_pred as ‘Predicted Values’ in a Pandas dataframe.
df = pd.DataFrame({'Real Values':y_test.reshape(-1), 'Predicted Values':y_pred.reshape(-1)})df>>Real Values    Predicted Values534.622865     510.602018542.608070     558.764770618.457277     653.356430460.402500     449.302331759.377432     728.037404631.318237     649.712332572.672047     583.685756494.627437     503.075097250.131728     239.372956594.651009     635.653662383.956240     384.531416491.230603     503.075097875.019348     933.984685273.073342     224.659296500.925064     498.355934191.623312     193.223331691.516541     726.817925421.621505     420.997198636.298374     653.945550321.848273     276.772845283.679657     275.805778608.936345     589.542982212.591740     239.372956594.804871     541.164031500.065779     524.649546
From the above values, we infer that the model is able to predict the values of the y_test with a good accuracy though it can be improved by tuning the hyper-parameters such as n_estimators and max_depth. I leave it to you all to play with those parameters and improve the accuracy of the Random Forest Regression Model.
In this graph, the Real values are plotted with “Red” color and the Predicted values are plotted with “Green” color. The plot of the Decision Tree Regression model is also drawn in “Black” color.
I am attaching a link of my github repository where you can find the Google Colab notebook and the data files for your reference.
github.com
I do hope that I have been able to explain the ML code for building a Decision Tree Regression model with an example.
You can also find the explanation of the program for other Regression models below:
Simple Linear Regression
Multiple Linear Regression
Polynomial Regression
Support Vector Regression
Decision Tree Regression
Random Forest Regression
We will come across the more complex models of Regression, Classification and Clustering in the upcoming articles. Till then, Happy Machine Learning!