forked from sandeepv59/Float_Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom_Forests_sample.R
170 lines (98 loc) · 4.08 KB
/
Random_Forests_sample.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Random forests - As ensemble learning method for classification and regression operate
# by constructing a multitude of decision trees
# why use random forests?
# Reasonable fast but very easy to use
# handles sparse data/missing data
# overcome problems with overfitting
# techniques used
# true bagging - random sample with replacement
# random subset of the features
# voting - allows different trees
data(iris)
library(randomForest)
# gives 100 samples between 1 to 150
s <- sample(150,100)
iris_train <- iris[s,]
iris_test <- iris[-s,]
# default no fo trees are 500, if we want to increase use ntree = 600
rfm <- randomForest(Species~.,iris_train)
p <- predict(rfm,iris_test)
table(iris_test[,5],p)
mean(iris_test[,5]==p)
# getting the importance of individual factor. So can be used as feature selection when using other algorithms
importance(rfm)
# if you want to know the structure of tree no 2
getTree(rfm,2)
# using float data
library(dplyr)
###############################Load Data from Local ETL##################
###############################Add your working directory######################
setwd('D:/Float/20160712')
#load('RiskView.Rdata')
#load('ubank.Rdata')
load('usum.Rdata')
# gives 500 samples between 1 to 664
s <- sample(dim(uval2)[1],dim(uval2)[1]*3/4)
uval2_dtree <- uval2[,-1]
uval2_dtree$approved <- as.factor(uval2_dtree$approved)
uval2_dtree$approved[uval2_dtree$approved == "TRUE"] <- c("Approved")
uval2_dtree$approved[uval2_dtree$approved == "FALSE"] <- c("NoTApproved")
uval_train <- uval2_dtree[s,-c(2:11)]
uval_test <- uval2_dtree[-s,-c(2:11)]
# default no fo trees are 500, if we want to increase use ntree = 600
str(uval_train)
rfm <- randomForest(approved ~ .,uval_train)
p <- predict(rfm,uval_test)
table(uval_test[,1],p)
mean(uval_test[,1]==p)
# getting the importance of individual factor. So can be used as feature selection when using other algorithms
a <- importance(rfm)
write.csv(a,'importance.csv')
# if you want to know the structure of tree no 2
getTree(rfm,2)
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(dim(uval2)[1],dim(uval2)[1]*3/4)
uval2_dtree <- uval2[,-1]
uval2_dtree$approved <- as.factor(uval2_dtree$approved)
uval2_dtree$approved[uval2_dtree$approved == "TRUE"] <- c("Approved")
uval2_dtree$approved[uval2_dtree$approved == "FALSE"] <- c("NoTApproved")
uval_train <- uval2_dtree[s,-c(2:11)]
uval_test <- uval2_dtree[-s,-c(2:11)]
# default no fo trees are 500, if we want to increase use ntree = 600
str(uval_train)
rfm <- randomForest(approved ~ .,uval_train)
p <- predict(rfm,uval_test)
table(uval_test[,1],p)
mean(uval_test[,1]==p)
# getting the importance of individual factor. So can be used as feature selection when using other algorithms
a <- importance(rfm)
write.csv(a,'importance.csv')
# if you want to know the structure of tree no 2
getTree(rfm,2)
#Regression
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(dim(uval2)[1],dim(uval2)[1]*3/4)
uval2_dtree <- uval2[,-c(1:4,6:12)]
uval_train <- uval2_dtree[s,-c(2:11)]
uval_test <- uval2_dtree[-s,-c(2:11)]
# default no fo trees are 500, if we want to increase use ntree = 600
str(uval_train)
rfm <- randomForest(approved ~ .,uval_train)
p <- predict(rfm,uval_test)
table(uval_test[,1],p)
mean(uval_test[,1]==p)
# getting the importance of individual factor. So can be used as feature selection when using other algorithms
a <- importance(rfm)
write.csv(a,'importance.csv')
# if you want to know the structure of tree no 2
getTree(rfm,2)