-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecision Tree Regression
More file actions
35 lines (29 loc) · 892 Bytes
/
Copy pathDecision Tree Regression
File metadata and controls
35 lines (29 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#reading the data
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset=pd.read_csv('Position_Salaries.csv')
X=dataset.iloc[:,1:2].values
Y=dataset.iloc[:,2].values
#fitting desicion tree
from sklearn.tree import DecisionTreeRegressor
regressor=DecisionTreeRegressor(random_state=0)
regressor.fit(X,Y)
#predict
Y_predict=regressor.predict(6.5)
#plot decision tree
plt.scatter(X,Y,color='red')
plt.plot(X,regressor.predict(X),color='blue')
plt.title('Truth or Bluff(Decision Tree Regression)')
plt.xlabel('level')
plt.ylabel('salary')
plt.show()
#plot decision tree for more resolution
X_grid=np.arange(min(X),max(X),0.01)
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('Truth or Bluff(Decision Tree Regression)')
plt.xlabel('position level')
plt.ylabel('salary')
plt.show()