-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecision Tree Regression
More file actions
31 lines (24 loc) · 1.03 KB
/
Copy pathDecision Tree Regression
File metadata and controls
31 lines (24 loc) · 1.03 KB
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
plot(Position_Salaries)
Position_Salaries<-Position_Salaries[2:3]
#fitting the Decision tree regression
library(rpart)
regressor=rpart(formula = Salary~.,data = Position_Salaries,control = rpart.control(minsplit = 1))
#predict
y_predict=predict(regressor,data.frame(Level=6.5))
#plotting the decision tree
library(ggplot2)
ggplot()+
geom_point(aes(x=Position_Salaries$Level,y=Position_Salaries$Salary),color='red')+
geom_line(aes(x=Position_Salaries$Level,y=predict(regressor,newdata = Position_Salaries)),color='blue')+
ggtitle('truth or bluff(decision tree regression)')+
xlab('level')+
ylab('salary')
#plotting the decision tree with high resolution
library(ggplot2)
x_grid=seq(min(Position_Salaries$Level),max(Position_Salaries$Level),0.1)
ggplot()+
geom_point(aes(x=Position_Salaries$Level,y=Position_Salaries$Salary),color='red')+
geom_line(aes(x=Position_Salaries$Level,y=predict(regressor,newdata = Position_Salaries)),color='blue')+
ggtitle('truth or bluff(decision tree regression)')+
xlab('level')+
ylab('salary')