问题描述
我有一个数据框,分为训练和测试数据集,运行支持向量机、预测和混淆矩阵函数。
如何查看哪些行是假阳性和假阴性?
Data_7c<- Data_7G[,c(6,15:18)]
split = sample.split(Data_7c$F,SplitRatio = 0.70)
train = subset(Data_7c,split == TRUE)
test = subset(Data_7c,split == FALSE)
data1 = svm(F ~.,data = train,method="C-classification",kernel="radial",cost = 1,gamma=0.001,scale = FALSE)
pred1 <- predict(data1,task = bh.task,newdata = test)
head(as.data.frame(pred1))
SVMcf1 <- confusionMatrix(pred1,as.factor(test[,1]),positive = "1")
SVMcf1$table
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 2203 146
1 3 2
数据 test$F[1:20]
和 pred1[1:20]
:
structure(c(1L,1L,2L,2L),.Label = c("0","1"),class = "factor")
structure(c(`2` = 1L,`3` = 1L,`8` = 1L,`19` = 1L,`22` = 1L,`25` = 1L,`40` = 1L,`49` = 1L,`51` = 1L,`55` = 1L,`57` = 1L,`60` = 1L,`62` = 1L,`63` = 1L,`67` = 2L,`72` = 1L,`75` = 1L,`80` = 1L,`81` = 1L,`89` = 1L),class = "factor")
解决方法
您可以使用逻辑运算符来确定误报和漏报,将它们存储在向量中,然后对数据进行子集化。
# convert factor to numeric 0/1
pred1 <- as.numeric(levels(pred1))[pred1]
test$F <- as.numeric(levels(test$F))[test$F]
false_positive <- pred1 & !test$F
false_negative <- !pred1 & test$F
false_positive
和 false_negative
将是逻辑向量。例如,在 false_positive
中,它将是 TRUE
,其中 pred1 == 1
和 test$F == 0
和 FALSE
否则。
然后可以使用这些逻辑向量对 test
数据集的行进行子集:
test[false_positive,]
test[false_negative,]
此外,根据混淆矩阵的输出,sum(false_positive)
应为 3,sum(false_negative)
应为 146。