Skip to content

Instantly share code, notes, and snippets.

@tharunpeddisetty
Last active August 22, 2020 02:08
Show Gist options
  • Save tharunpeddisetty/8c3213de90fdc50c5814dbadcba181ac to your computer and use it in GitHub Desktop.
Save tharunpeddisetty/8c3213de90fdc50c5814dbadcba181ac to your computer and use it in GitHub Desktop.
Implementing Radom forest regression in Python
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('/Users/tharunpeddisetty/Desktop/Python/Position_Salaries.csv')
X = dataset.iloc[:,1:-1].values
y = dataset.iloc[:, -1].values
#Training the Random Forest Regression Model
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(random_state=0,n_estimators=10)
regressor.fit(X,y)
#Predicting
regressor.predict([[6.5]])
#Visualizing the Regression Results in High resolution. If we have dimensions > 2, we can't really plot it. Also, it makes no sense.
X_grid = np.arange(min(X), max(X), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid,regressor.predict(X_grid), color = 'blue')
plt.title('Random Forest Tree')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
Position Level Salary
Business Analyst 1 45000
Junior Consultant 2 50000
Senior Consultant 3 60000
Manager 4 80000
Country Manager 5 110000
Region Manager 6 150000
Partner 7 200000
Senior Partner 8 300000
C-level 9 500000
CEO 10 1000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment