forked from sandeepv59/Float_Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecision_trees_sample_rpart.R
More file actions
66 lines (39 loc) · 1.37 KB
/
Decision_trees_sample_rpart.R
File metadata and controls
66 lines (39 loc) · 1.37 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
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
66
# decision trees using rpart
data(iris)
dim(iris)
library(rpart)
# gives 100 samples between 1 to 150
s <- sample(150,100)
iris_train <- iris[s,]
iris_test <- iris[-s,]
dtm <- rpart(Species~.,iris_train, method = "class")
plot(dtm)
# but it doesn't give text
# to add text
text(dtm)
# still it is bad. hence we can use library rpart.plot which gives better picture
library(rpart.plot)
rpart.plot(dtm)
# lets predict using the formula
p <- predict(dtm, iris_test, type = "class")
table(iris_test[,5],p)
# limitations
# Good representative train data
# Fairly simple
# Other ensemble techniques like random forest more elegant and sophisticated amchine learnign algorithms
# using float data
library(dplyr)
###############################Load Data from Local ETL##################
###############################Add your working directory######################
setwd('D:/Float/Internship Materials/Data')
load('uval2.Rdata')
# gives 500 samples between 1 to 664
s <- sample(length(uval2),length(uval2)*3/4)
uval2_dtree <- uval2[,-1]
uval_train <- uval2_dtree[s,-c(2:11)]
uval_test <- uval2_dtree[-s,-c(2:11)]
dtm_uval <- rpart(approved ~ .,uval_train, method = "class")
rpart.plot(dtm_uval, type = 4, extra = 101)
p <- predict(dtm_uval, uval_test, type = "class")
table(uval_test[,1],p)
mean(uval_test[,1] == p)