I am building a Deep Learning model for intrusion detection using UNSW18 dataset but at the confusion matrix stage, I kept having this error, Error in unique.default(x, nmax = nmax) : unique() applies only to vectors
. Everything seems ok up until the confusion matrix. Can someone help me with what I can do. The dataset has 22 columns and over 1 million rows. The 22nd column is the target variable. Below is code I used:
#convert datafarame to matrix and remove dimensions
DF <- as.matrix(new_UNSW2A)
dimnames(DF) <-NULL
set.seed(222)
#data partition
part <- sample(1:1999934, 0.97*nrow(DF), replace = F)
training<- DF[part,1:21]
testing <- DF[-part,1:21]
trainingtarget <- DF[part,22]
testingtarget <- DF[-part,22]
trainLabels <- to_categorical(trainingtarget)
testLabels <- to_categorical(testingtarget)
testLabels[1:10,]
#build model
model <- keras_model_sequential()
model%>%layer_dense(units = 8, activation = "relu", input_shape = c(21)) %>%
layer_dense(units = 2, activation = "sigmoid")
summary(model)
# compile the loss function
model %>% compile(loss= "binary_crossentropy",
optimizer="adam",
metrics=c("accuracy")
)
#fit model
history <- model %>% fit(training, trainLabels, epochs=20, batch_size=32,
validation_split=0.03,)
plot(history)
# model evaluation
model %>% evaluate(testing, testLabels)
pred <- model %>% predict(testing) %>% `>`(0.5) %>% k_cast("int32")
#table(Predicted=pred, Actual=testingtarget)
confusionMatrix(pred, as.factor(testingtarget))
The error I get is and even when I checked the levels, they're the same.
> confusionMatrix(pred, as.factor(testingtarget))
Error: `data` and `reference` should be factors with the same levels.
A dput output is here below
structure(list(Feature1 = c(0.454545454545455, 0.454545454545455,
0.454545454545455, 0.779220779220779, 0.441558442, 0.779220779220779,
0.441558441558442, 0, 0.454545454545455, 0.233766233766234, 0.233766233766234,
0.233766233766234, 0.454545454545455, 0.779220779220779, 0.441558442,
0.233766233766234, 0.454545454545455, 0.441558442, 0, 0.051948052,
0.441558442, 0.441558441558442, 0.051948052, 0.246753247, 0.831168831,
0.779220779, 1, 0.779220779, 0.779220779220779, 0.454545454545455
), Feature2 = c(0.467673762111849, 0.580117494468605, 0.358022430762188,
0.263675898374914, 0.123292897, 0.348760204470893, 0.0850385290302892,
0.944426642252232, 0.43228809033341, 0.822384985122454, 0.252170595864805,
0.374792095826658, 0.0988479438467994, 0.877973601892119, 0.005569543,
0.103913939116503, 0.35048447394522, 0.005569543, 0.511757076371405,
0.001220722, 0.001220722, 0.780697337300679, 0.001220722, 0.001220722,
0.123292897, 0.630304417, 0.237918669413291, 0.001220722, 0.435034714274815,
0.815045395590143), Feature3 = c(0.00390625, 0.00390625, 0.00390625,
0.00390625, 0.433740234, 0.00390625, 0.00390625, 0.00390625,
0.00390625, 0.00390625, 0.00390625, 0.00390625, 0.00390625, 0.00390625,
0.027587891, 0.00390625, 0.00390625, 0.027587891, 0.00390625,
0.00390625, 0.00390625, 0.00390625, 0, 0, 0.00390625, 0.00390625,
0.00390625, 0, 0.00390625, 0.00390625), Feature4 = c(0.00221116639027087,
0.00221116639027087, 0.00165837479270315, 0.00276395798783858,
0.004975124, 0.00248756218905473, 0.00248756218905473, 0.00248756218905473,
0.00165837479270315, 0.00248756218905473, 0.00276395798783858,
0.00248756218905473, 0.00193477059148701, 0.00248756218905473,
0.002763958, 0.00193477059148701, 0.00276395798783858, 0.004975124,
0.00248756218905473, 0.299613046, 0.348811498, 0.00276395798783858,
0.499170813, 0.208955224, 1, 0.705085683, 0.00248756218905473,
0.20923162, 0.00138197899391929, 0.00221116639027087), Feature5 = c(0.00013617403722828,
0.00013617403722828, 0.00010213052792121, 0.00017021754653535,
0.011625858, 0.000153195791881815, 0.000153195791881815, 0.000153195791881815,
0.00010213052792121, 0.000153195791881815, 0.00017021754653535,
0.000153195791881815, 0.000119152282574745, 0.000153195791881815,
0.001879769, 0.000119152282574745, 0.00017021754653535, 0.01154075,
0.000153195791881815, 0.219358217, 0.219358217, 0.00017021754653535,
0.480314766, 0.154139364, 0.492898098, 0.492898098, 0.000153195791881815,
0.051442012, 8.51087732676748e-05, 0.00013617403722828), Feature6 = c(0.584269662921348,
0.595505617977528, 0.584269662921348, 0.786516853932584, 0.606741573,
0.786516853932584, 0.595505617977528, 0.123595505617978, 0.584269662921348,
0.314606741573034, 0.303370786516854, 0.314606741573034, 0.595505617977528,
0.786516853932584, 0.606741573, 0.292134831460674, 0.595505617977528,
0.606741573, 0.112359550561798, 0.314606742, 0.595505618, 0.595505617977528,
0.314606742, 0.314606742, 0.797752809, 0.797752809, 0.955056179775281,
0.764044944, 0.786516853932584, 0.584269662921348), Feature7 = c(0.523008849557522,
0.756145102227647, 0.790021361000915, 0.479451480012206, 0.999977113,
0.401930119011291, 0.14476274031126, 0.705836130607263, 0.877181873664937,
0.466638693927373, 0.344491913335368, 0.404298901434239, 0.531484589563625,
0.672425236496796, 0.999977113, 0.76296917912725, 0.284406469331706,
0.976373207, 0.348695453158377, 0.00284559, 0.002502289, 0.0209261519682637,
0.003394873, 0.002853219, 0.001754654, 0.001754654, 0.0672833384192859,
0.017889838, 0.3379729935917, 0.791344980164785), Feature8 = c(0.924067284093511,
0.933553433304087, 0.880932504602948, 0.643121919935511, 0.999985098,
0.646725827978733, 0.955242795653076, 0.642291962202527, 0.88080564363374,
0.642004139972512, 0.626099685126186, 0.626036491175397, 0.968608335956231,
0.629381158157862, 0.999998541, 0.525018812322718, 0.95566169703866,
0.999984941, 0.641129359081669, 0.670425964, 0.999999527, 0.990344926223526,
0.672462915, 0.591780403, 0.682724777, 0.394768668, 0.56618559105385,
0.682749415, 0.610435706324662, 0.902571445827134), Feature9 = c(0.464488019252737,
0.670636068908727, 0.570118599792263, 0.609440553029923, 0.779655927,
0.891778885283119, 0.702606716297148, 0.885368812830269, 0.565040961488946,
0.884127452926373, 0.862174573983775, 0.862007372031717, 0.747950331255167,
0.868151993948047, 0.394116723, 0.685676472393543, 0.59972191465566,
0.74300482, 0.606336655052209, 0.786637356, 0.900535963, 0.780801230159964,
0.786637356, 0.784755885, 0.858226296, 0.860693073, 0.763340683044885,
0.860693073, 0.734019957416313, 0.534261852207026), Feature10 = c(0.609519970062372,
0.539096682787955, 0.247021032429909, 0.786199589826012, 0.357302125,
0.0180044043199535, 0.57856736344628, 0.0130171727540883, 0.236862351429847,
0.0133246475053385, 0.00698915472921531, 0.00825509636062917,
0.455559664118319, 0.0104390125388293, 0.356658697, 0.615631197607482,
0.819405973019647, 0.358014968, 0.78216459555056, 0.795745101,
0.521243124, 0.338854084764248, 0.796485977, 0.796645277, 0.517766123,
0.521198181, 0.301311907104358, 0.422339042, 0.46436429928907,
0.331942354936216), Feature11 = c(0.411336187762334, 0.593774068481957,
0.504817620540601, 0.539616974828479, 0.020977311, 0.592089153493137,
0.622067712647877, 0.587834626303721, 0.500324072988115, 0.587010643174212,
0.572439616163544, 0.572328681791013, 0.662196061424133, 0.576407063186208,
0.035810021, 0.455290978742752, 0.531016165907028, 0.657819401,
0.536870070453027, 0.696433601, 0.997622142, 0.69126867123373,
0.694768527, 0.694765, 0.760926418, 0.800919318, 0.506839703711751,
0.762069889, 0.487378305081517, 0.473084966734679), Feature12 = c(0,
0.357501228725261, 0.488197565678423, 0, 0.189424512, 0.987592877174177,
0.302463010365771, 0.983498386533616, 0.488299866786166, 0.978043735947497,
0.957961806103818, 0.957235468238842, 0.490582515851112, 0.962856469321899,
0.002625803, 0.330538437417297, 0, 0.189474995, 0, 0.189333108,
0.704987624, 0.675012954433752, 0.186600112, 0.181129449, 0.512850798,
0.512850798, 0.63936390950355, 0.512854579, 0.491362895388222,
0.325401365052607), Feature113 = c(0.462823054509754, 0.631043808161399,
0.460977611877408, 0.540471090216139, 0.030554025, 0.596668801577587,
0.632712541267336, 0.590441041696074, 0.445073754500694, 0.588176473562569,
0.571959358389041, 0.572474344199609, 0.642264994764552, 0.577391003667974,
0.658106569, 0.584975818973599, 0.636556170981083, 0.654725782,
0.534700246919302, 0.655010992, 0.65694716, 0.64304043619601,
0.658073124, 0.656549498, 0.658047315, 0.658047842, 0.569330705426536,
0.65694716, 0.582531907788703, 0.45043692813005), Feature14 = c(0.00496963003865268,
0.00496963003865268, 0.00386526780784097, 0.00607399226946438,
0.00496963, 0.00552181115405853, 0.00552181115405853, 0.00552181115405853,
0.00386526780784097, 0.00552181115405853, 0.00607399226946438,
0.00552181115405853, 0.00441744892324683, 0.00552181115405853,
0.235229155, 0.00441744892324683, 0.00607399226946438, 0.235229155,
0.00552181115405853, 0.703478741, 1, 0.00607399226946438, 0.697404749,
0.00496963, 0.697404749, 0.705687465, 0.00552181115405853, 0.234676974,
0.00331308669243512, 0.00496963003865268), Feature15 = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.003318584, 0, 0, 0.005530973,
0, 0, 0, 0, 0, 0, 1, 0.700221239, 0, 0, 0, 0), Feature16 = c(0.000296444803350046,
0.000296444803350046, 0.000230568180383369, 0.000362321426316723,
0.031950162, 0.000329383114833384, 0.000329383114833384, 0.000329383114833384,
0.000230568180383369, 0.000329383114833384, 0.000362321426316723,
0.000329383114833384, 0.000263506491866707, 0.000329383114833384,
0.031950162, 0.000263506491866707, 0.000362321426316723, 0.022365113,
0.000329383114833384, 0.667110602, 0.92947633, 0.000362321426316723,
0.653628951, 0.298303622, 0.659846606, 0.424506763, 0.000329383114833384,
0.296242782, 0.000197629868900031, 0.000296444803350046), Feature17 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000366559, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Feature18 = c(0.000328775363031432,
0.00032077855108013, 0.000183852877076748, 0.000923821399730764,
0.002136038, 0.000791178048251419, 0.000387684459428396, 0.000799802141529841,
0.000183945124330736, 0.000800365874748659, 0.000961392681310732,
0.000832463819258652, 0.000209161423693198, 0.00082560677337885,
0.001793114, 0.00077296639037514, 0.000471869303418169, 0.001707976,
0.00080208372361182, 0.079960309, 0.048996252, 0.00043929782300437,
0.15130476, 0.077924453, 0.143547329, 0.143547329, 0.000968885208273564,
0.077924453, 0.000335970648842524, 0.000347517955102888), Feature19 = c(0.00518582702163804,
0.00513313425797405, 0.00423090068993076, 0.00910671704280376,
0.006488045, 0.00823270064556132, 0.00557399209454559, 0.00828952670470883,
0.00423150852750187, 0.00829324126764336, 0.00935428253176397,
0.00850474172738469, 0.00439766430443877, 0.00845955913459923,
0.005300762, 0.00811270000152365, 0.00612870466193587, 0.008097288,
0.00830456055396749, 0.599799405, 0.591129926, 0.00591408396932954,
1, 0.599799405, 0.986516204, 0.999991855, 0.00940365245003926,
0.507805418, 0.00523323835218422, 0.00530932610858497), Feature20 = c(0,
0, 0, 0, 0.006764518, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.006764518,
0, 0, 0.006512343, 0, 0, 0, 0, 0, 0, 0.965209157, 0.965209157,
0, 0, 0, 0), Feature21 = c(1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1), Label.1 = c(0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0,
1, 1, 1, 1, 0, 1, 0, 0)), row.names = c(571506L, 632625L, 641506L,
822246L, 1099223L, 801923L, 472345L, 95118L, 664356L, 294567L,
262545L, 278224L, 573728L, 872836L, 1309515L, 372253L, 508954L,
1189655L, 1490L, 1340533L, 1317756L, 439880L, 1365984L, 1158474L,
1359139L, 1209880L, 976353L, 1745320L, 785156L, 641853L), class = "data.frame")
CodePudding user response:
You could convert your pred
to a vector like this:
# model evaluation
model %>% evaluate(testing, testLabels)
#pred <- model %>% predict(testing) %>% `>`(0.5) %>% k_cast("int32")
pred <- model %>% predict(testing) %>% as.data.frame() %>%
mutate(target = ifelse(V1 > 0.5, 1, 0)) %>% select(target) %>% as.vector()
#table(Predicted=pred, Actual=testingtarget)
confusionMatrix(table(pred$target, as.factor(testingtarget)))
Output:
Confusion Matrix and Statistics
0 1
0 0 1
1 20 9
Accuracy : 0.3
95% CI : (0.1473, 0.494)
No Information Rate : 0.6667
P-Value [Acc > NIR] : 1
Kappa : -0.0678
Mcnemar's Test P-Value : 8.568e-05
Sensitivity : 0.00000
Specificity : 0.90000
Pos Pred Value : 0.00000
Neg Pred Value : 0.31034
Prevalence : 0.66667
Detection Rate : 0.00000
Detection Prevalence : 0.03333
Balanced Accuracy : 0.45000
'Positive' Class : 0