-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVM-IntrusionAttack.R
85 lines (54 loc) · 1.76 KB
/
SVM-IntrusionAttack.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
##############################################################
library(caret)
data <- read.csv(file.choose())
col <- c("SameSrvRate", "LoggedIn", "DstHostSameSrvRate",
"DstHostSrvCount","Flag","Attack" )
inTrain <- createDataPartition(y=data$Attack, p=0.1, list=FALSE)
#str (data)
trainData <- data[inTrain,col]
testData <- data[-inTrain,col]
#dim <- nrow(trainData)
#dim(trainData)
##############################################################
#SVM
library(e1071)
rbf.tune = tune.svm(Attack ~., data = trainData, kernel="radial",
gamma=c(0.1,0.5,1,2,3,4))
summary(rbf.tune)
#choose the best gamma value
best.rbf = rbf.tune$best.model
## plot the model
library(ggplot2)
tune.test = predict(best.rbf , testData) ## predic the model
t <- table(tune.test, testData$Attack) #c onfusion matrix
# plot the predicted model
t1 <- as.numeric(tune.test) # # convert tune.test into numeric to plot the graph
# attacks after training data
qplot(t1,
geom="histogram",
binwidth = 0.5,
main = "Histogram for Attack After training",
xlab = "Attack",
ylab = "Frequency",
fill=I("blue"),
col=I("red"),
alpha=I(.2))
# attacks before traning data
qplot(as.numeric(data$Attack),
geom="histogram",
binwidth = 0.5,
main = "Histogram for Attack Before Tranining",
xlab = "Attack",
ylab = "Frequency",
fill=I("blue"),
col=I("red"),
alpha=I(.2),)
#overall accuracy
sum(diag(t)/sum(t))
#misclassification
1 - sum(diag(t)/sum(t)))
## confusion matrix
confusionMatrix(tune.test , testData$Attack)
##############################################################
# https://rpubs.com/Kushan/296706
# https://www.youtube.com/watch?v=RNpglFQYwZI