Decision Tree and Random Forest regression

Today we will code Decision Tree and Random Forest regression model.

  • Decision Tress Regression Model

Check this out on Google Colab Notebook.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Importing the data set
dataSet = pd.read_csv('Position_salaries.csv')
x = dataSet.iloc[:, 1:2].values
y = dataSet.iloc[:, 2].values

#Fitting the Decision Tree regression model into the dataset
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(random_state = 0)
regressor.fit(x, y)
y_pred = regressor.predict(6.5)

# Visualising the Decision Tree Regression results ( for higher resolution and smoother curve)
X_grid = np.arange(min(x), max(x), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(x, y, color = 'blue')
plt.title('Truth or Bluff(Decision Tree Regression')
plt.plot(X_grid, regressor.predict(X_grid), color = 'red')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()
  • Random Forest Regression

Check this out on Google Colab Notebook

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Importing the data set
dataSet = pd.read_csv('Position_salaries.csv')
x = dataSet.iloc[:, 1:2].values
y = dataSet.iloc[:, 2].values

#Fitting the Random forest regression model into the dataset
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 300, random_state = 0)
regressor.fit(x, y)

y_pred = regressor.predict(6.5) 

# Visualising the Random forest Regression results ( for higher resolution and smoother curve)
X_grid = np.arange(min(x), max(x), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(x, y, color = 'blue')
plt.title('Truth or Bluff(Random forest Regression')
plt.plot(X_grid, regressor.predict(X_grid), color = 'red')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()

The dataset is available here.

NOTE: Detailed explanation will be coming soon.

Leave a Reply

Close Menu