-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecision Tree Regression.R
65 lines (48 loc) · 2.38 KB
/
Decision Tree Regression.R
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# ---------------------------------------------------- Importing Data ------------------------------------------ #
dataset = read.csv("Position_Salaries.csv")
# Selecting particular columns
dataset = dataset[2:3]
# Level is the independent variable.
# Salary is the dependent variable.
# ---------------------------------- Fitting Support Vector Regression to the dataset -------------------------- #
library(rpart)
reg = rpart(Salary ~. ,
data = dataset)
summary(reg)
# -------------------------------------------- Predictiing a new result --------------------------------------- #
y_pred = predict(reg, data.frame(Level = 6.5))
y_pred
# --------------------------------- Visualising the Decision Tree Regression results --------------------------- #
library(ggplot2)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary), colour = 'red') +
geom_line(aes(x = dataset$Level, y = predict(reg, newdata = dataset)), colour = 'blue') +
ggtitle("Truth or Bluff (Decision Tree Regression)") +
xlab("Position Level") +
ylab("Salaray")
# We got the straight line because we didnt apply feature scaling in our dataset, but we dont need to appply
# feature scaling because it is based on Euclidean distance and decision tree regression are based on condition
# on the independent variable.
# So for making a better visualisation there a function in rpart which can help in splitting the dataset.
library(rpart)
reg_1 = rpart(Salary ~. ,
data = dataset,
control = rpart.control(minsplit = 1))
summary(reg)
library(ggplot2)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary), colour = 'red') +
geom_line(aes(x = dataset$Level, y = predict(reg_1, newdata = dataset)), colour = 'blue') +
ggtitle("Truth or Bluff (Decision Tree Regression)") +
xlab("Position Level") +
ylab("Salaray")
# This is a new regression which is non-linear and non-continuous regression model.
# ------------------ Visualising the DTR Model results (for higher resolution and smoother curve) -------------- #
library(ggplot2)
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.01)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary), colour = 'red') +
geom_line(aes(x = x_grid, y = predict(reg_1, newdata = data.frame(Level = x_grid))), colour = 'blue') +
ggtitle('Truth or Bluff (Decision Tree Regression)') +
xlab('Level') +
ylab('Salary')