-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_rf.Rmd
236 lines (151 loc) · 5.71 KB
/
model_rf.Rmd
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
## Building the model using random forest classifier
```{r}
library(randomForest)
library(caret)
library(e1071)
```
## creating one more temporary table called kddcopy modifying result column of that to labels
#number of attributes: 6 training data size: 1262 , testing data size: 124711
```{r}
data1<-kddcopy[,c("srv_rerror_rate", "rerror_rate", "flag", "dst_host_rerror_rate", "logged_in", "protocol_type","result" )]
inTrain <- createDataPartition(y=data1$result,p=0.01, list=FALSE)
training <- data1[inTrain,]
testing <- data1[-inTrain,]
dim(training)
modFit <- train(result ~ .,method="rf",data=training)
modFit
pred <- predict(modFit,testing)
testing$predRight <- pred==testing$result
answer<-table(pred,testing$result)
round(prop.table(answer,1)*100,2)
```
Output:
>answer
pred dos normal probe r2l u2r
dos 41277 2377 576 0 0
normal 1231 62162 1209 928 51
probe 2959 2130 9754 57 0
r2l 0 0 0 0 0
u2r 0 0 0 0 0
> round(prop.table(answer,1)*100,2)
pred dos normal probe r2l u2r
dos 93.32 5.37 1.30 0.00 0.00
normal 1.88 94.79 1.84 1.42 0.08
probe 19.86 14.30 65.46 0.38 0.00
r2l
u2r
#number of attributes: 5 training data size: 1262 , testing data size: 124711
```{r}
data1<-kddcopy[,c("srv_rerror_rate", "rerror_rate", "flag", "dst_host_rerror_rate", "logged_in","result" )]
inTrain <- createDataPartition(y=data1$result,p=0.01, list=FALSE)
training <- data1[inTrain,]
testing <- data1[-inTrain,]
dim(training)
modFit <- train(result ~ .,method="rf",data=training)
modFit
pred <- predict(modFit,testing)
testing$predRight <- pred==testing$result
answer<-table(pred,testing$result)
round(prop.table(answer,1)*100,2)
```
pred dos normal probe r2l u2r
dos 93.04 5.41 1.55 0.00 0.00
normal 5.44 86.20 7.03 1.26 0.07
probe 3.05 10.85 85.29 0.81 0.00
r2l
u2r
#readin test data
#number of attributes: 6 training data size: 12600 , testing data size: 113373
```{r}
data1<-kddcopy[,c("srv_rerror_rate", "rerror_rate", "flag", "dst_host_rerror_rate", "logged_in","result" )]
inTrain <- createDataPartition(y=data1$result,p=0.1, list=FALSE)
training <- data1[inTrain,]
testing <- data1[-inTrain,]
dim(training)
modFit <- train(result ~ .,method="rf",data=training)
modFit
pred <- predict(modFit,testing)
testing$predRight <- pred==testing$result
answer<-table(pred,testing$result)
round(prop.table(answer,1)*100,2)
```
pred dos normal probe r2l u2r
dos 94.32 5.23 0.45 0.00 0.00
normal 5.23 86.31 7.13 1.26 0.07
probe 6.91 7.76 85.13 0.19 0.02
r2l 0.00 0.00 23.40 76.60 0.00
#number of attributes: 6 training data size: 25197 , testing data size: 100776
```{r}
data1<-kddcopy[,c("srv_rerror_rate", "rerror_rate", "flag", "dst_host_rerror_rate", "logged_in","result" )]
inTrain <- createDataPartition(y=data1$result,p=0.2, list=FALSE)
training <- data1[inTrain,]
testing <- data1[-inTrain,]
dim(training)
modFit <- train(result ~ .,method="rf",data=training)
modFit
pred <- predict(modFit,testing)
testing$predRight <- pred==testing$result
answer<-table(pred,testing$result)
round(prop.table(answer,1)*100,8)
```
TIme taken: 11:50 to 12:18
pred dos normal probe r2l u2r
dos 94.31 5.25 0.44 0.00 0.00
normal 5.13 86.36 7.17 1.26 0.07
probe 7.18 6.18 86.55 0.09 0.00
r2l 0.00 3.64 32.73 63.64 0.00
u2r
#number of attributes: 9 training data size: 12600 , testing data size: 113373
```{r}
data1<-kddcopy[,c("srv_rerror_rate", "protocol_type" ,"src_bytes", "rerror_rate", "flag", "dst_host_rerror_rate", "logged_in","result","service" ,"serror_rate" )]
inTrain <- createDataPartition(y=data1$result,p=0.1, list=FALSE)
training <- data1[inTrain,]
testing <- data1[-inTrain,]
dim(training)
modFit <- train(result ~ .,method="rf",data=training)
modFit
pred <- predict(modFit,testing)
testing$predRight <- pred==testing$result
answer<-table(pred,testing$result)
round(prop.table(answer,1)*100,2)
```
pred dos normal probe r2l u2r
dos 98.92 0.53 0.55 0.00 0.00
normal 0.36 98.86 0.23 0.47 0.07
probe 0.00 2.37 97.54 0.07 0.02
r2l 0.00 6.86 0.00 93.14 0.00
u2r
#number of attributes: 9 training data size: 12625 , testing data size: 124711 and test data as test.final
```{r}
data1<-kddcopy[,c("srv_rerror_rate", "protocol_type" ,"src_bytes", "rerror_rate", "flag", "dst_host_rerror_rate", "logged_in","result","service" ,"serror_rate" )]
inTrain <- createDataPartition(y=data1$result,p=0.1, list=FALSE)
training <- data1[inTrain,]
testing <- data1[-inTrain,]
dim(training)
modFit <- train(result ~ .,method="rf",data=training)
modFit
pred <- predict(modFit,test.final)
testing$predRight <- pred==test.final$result
answer<-table(pred,test.final$result)
round(prop.table(answer,1)*100,2)
```
1262 samples
9 predictor
5 classes: 'dos', 'normal', 'probe', 'r2l', 'u2r'
No pre-processing
Resampling: Bootstrapped (25 reps)
Summary of sample sizes: 1262, 1262, 1262, 1262, 1262, 1262, ...
Resampling results across tuning parameters:
mtry Accuracy Kappa
2 0.8523705 0.7218909
44 0.9785942 0.9623108
87 0.9752067 0.9568197
Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mtry = 44.
> round(prop.table(answer,1)*100,2)
pred dos normal probe r2l u2r
dos 96.82 1.68 1.50 0.00 0.00
normal 0.75 80.21 0.23 18.50 0.31
probe 23.09 12.35 64.49 0.06 0.00
r2l 0.00 10.71 0.00 89.29 0.00
u2r