Skip to content

Instantly share code, notes, and snippets.

@tharunpeddisetty
Last active July 21, 2020 05:21
Show Gist options
  • Save tharunpeddisetty/433e5fe5af0e6b6cdd9d7df3339931a5 to your computer and use it in GitHub Desktop.
Save tharunpeddisetty/433e5fe5af0e6b6cdd9d7df3339931a5 to your computer and use it in GitHub Desktop.
Implementing Decision tree regression.
# 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/Position_Salaries.csv') #copy you file path
X = dataset.iloc[:,1:-1].values
y = dataset.iloc[:, -1].values
#Training the Decision Tree Model
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(random_state=0)
regressor.fit(X,y)
#Prediction for level 6.5
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('Decision Tree')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
#visualizing normally makes no sense.Since predicted would be the same value; we used the entire data to run this regression
plt.scatter(X, y, color = 'red')
plt.plot(X,regressor.predict(X), color = 'blue')
plt.title('Decision 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