What comes to mind when we think of a forest after knowing about a tree ? In previous article we learned about decision tree regression and now will learn about random forest regression.
Random Forest Regression:
It uses ensemble learning. It either takes multiple algorithms or one algorithm multiple times and calculates predictions by taking average of predictions of algorithms. Random forest regression takes an input parameter to tell the number of decision trees.
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
# Fitting Random Forest Regression to the dataset
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 10000, random_state = 0)
# Predicting a new result
y_pred = regressor.predict(8.3)
- We imported RandomForestRegressor.
- Created regressor object with 1000 estimators. It is like creating 1000 decision trees for random forest.
- fit X and y in regressor.
After fitting data in RandomForestRegressor, we simply predicted value of y_pred and it came to be 193113 which is a quite close prediction to actual value 190000.