【问题标题】:Plot error SVM...min not meaningful for factors绘制错误 SVM...min 对因子没有意义
【发布时间】:2014-07-06 03:52:52
【问题描述】:

我正在尝试训练 SVM 进行异常检测。为此,我仅使用 sourceip 和协议创建了 train_data 和 test_data。但是,当我尝试使用绘图功能时,它给了我以下错误...

> plot(svmfit,testdat)
Error in Summary.factor(c(7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,  : 
  min not meaningful for factors

我怎样才能摆脱这个错误..?

以下是外部文件中的命令行

    train_data=read.csv("packetcapture_training.csv")
    #read only source ip and protocol  
    xtrain=train_data[4:23,c(3,5)]
    ytrain=c(rep(-1,10),rep(1,10))
    dat=data.frame(x=xtrain,y=as.factor(ytrain))
    library("e1071")
    svmfit=svm(y~.,data=dat,kernel="radial",cost=10,scale=FALSE)
    summary(svmfit)
    test_data=read.csv("packetcapture_testing.csv")
    #read only source ip and protocol
    xtest=test_data[371:390,c(3,5)]
    ytest=c(rep(1,10),rep(-1,10))
    testdat=data.frame(x=xtest,y=as.factor(ytest))
    plot(svmfit,testdat)





    > dat
                   x.Source x.Protocol  y
1  fe80::a00:27ff:feee:7ec6     ICMPv6 -1
2  fe80::a00:27ff:feee:7ec6     ICMPv6 -1
3  fe80::a00:27ff:feee:7ec6     ICMPv6 -1
4               172.16.11.1        TCP -1
5             192.168.2.101        TCP -1
6               172.16.11.1        TCP -1
7               172.16.11.1        TCP -1
8               172.16.11.1        TCP -1
9             192.168.2.101        TCP -1
10            192.168.2.101        TCP -1
11              172.16.11.1        TCP  1
12              172.16.11.1        TCP  1
13              172.16.11.1        TCP  1
14            192.168.2.101        TCP  1
15              172.16.11.1        TCP  1
16            192.168.2.101        TCP  1
17              172.16.11.1        TCP  1
18              172.16.11.1        TCP  1
19            192.168.2.101      SSHv2  1
20              172.16.11.1        TCP  1

> dput(head(dat,4))
structure(list(x.Source = structure(c(6L, 6L, 6L, 1L), .Label = c("172.16.11.1", 
"192.168.2.100", "192.168.2.101", "CadmusCo_8b:7b:80", "CadmusCo_ee:7e:c6", 
"fe80::a00:27ff:feee:7ec6"), class = "factor"), x.Protocol = structure(c(5L, 
5L, 5L, 7L), .Label = c("ARP", "DNS", "HTTP", "ICMP", "ICMPv6", 
"SSHv2", "TCP", "UDP"), class = "factor"), y = structure(c(1L, 
1L, 1L, 1L), .Label = c("-1", "1"), class = "factor")), .Names = c("x.Source", 
"x.Protocol", "y"), row.names = c(NA, 4L), class = "data.frame")

> testdat
         x.Source x.Protocol  y
371   172.16.11.1        TCP  1
372   172.16.11.1        TCP  1
373   172.16.11.1        TCP  1
374   172.16.11.1        TCP  1
375   172.16.11.1        TCP  1
376   172.16.11.1        TCP  1
377   172.16.11.1        TCP  1
378   172.16.11.1        TCP  1
379   172.16.11.1        TCP  1
380   172.16.11.1        TCP  1
381   172.16.11.1        TCP -1
382   172.16.11.1        TCP -1
383   172.16.11.1        TCP -1
384   172.16.11.1        TCP -1
385   172.16.11.1        TCP -1
386   172.16.11.1        TCP -1
387   172.16.11.1        TCP -1
388   172.16.11.1        TCP -1
389 192.168.2.101      SSHv2 -1
390 192.168.2.101     ICMPv6 -1


> dput(head(testdat,4))
structure(list(x.Source = structure(c(1L, 1L, 1L, 1L), .Label = c("172.16.11.1", 
"192.168.2.100", "192.168.2.101", "CadmusCo_8b:7b:80", "CadmusCo_ee:7e:c6", 
"fe80::a00:27ff:feee:7ec6"), class = "factor"), x.Protocol = structure(c(7L, 
7L, 7L, 7L), .Label = c("ARP", "DNS", "HTTP", "ICMP", "ICMPv6", 
"SSHv2", "TCP", "UDP"), class = "factor"), y = structure(c(2L, 
2L, 2L, 2L), .Label = c("-1", "1"), class = "factor")), .Names = c("x.Source", 
"x.Protocol", "y"), row.names = 371:374, class = "data.frame")

【问题讨论】:

  • 因为您没有包含任何数据,所以此错误无法重现。如果您想让人们更容易地帮助您,请参阅How to make a great R reproducible example?上的指南
  • 抱歉信息较少。我不确定这是否足够...我想,我在列格式方面犯了一个错误...我应该如何格式化 IP 地址以进行 SVM 学习。 ..

标签: r plot svm


【解决方案1】:

library("e1071") 中的 plot.svm 函数显然只喜欢绘制连续预测变量。因为您的模型使用两个分类预测变量,所以您会遇到该错误。你知道你期待什么样的可视化吗?

在帮助页面的示例中,它显示

data(cats, package = "MASS")
m <- svm(Sex~., data = cats)
plot(m, cats)

它可以在一个范围内分散点,并且切割可以在有意义的断点处发生。对于分类预测变量,它们没有排序,因此没有明确的方法以类似的方式绘制它们。

【讨论】:

  • 感谢您的回答。我只是希望它应该用分类来绘制数据点,以便我可以在图中看到两个清晰的类。它正在将 SourceIp 视为分类数据,因为我将它传入字符串格式。我知道不应该是这样,问题就出在这里。我不知道我应该如何从 csv 文件中输入 ip 地址来创建一个 svm 模型。
  • @user3648560 您将所有内容都作为factor 传递。考虑到您的数据,这似乎是正确的选择。为离散数据绘制图表更加困难。没有明显的方法可以“展开”数据来查看它。
  • 好的。谢谢你。我将源 Ip 更改为四个不同的八位字节并将其作为数值数据传递。当我使用预测函数进行预测时,它会给出错误“测试数据与模型不匹配”。所有命令都相同,csv 文件现在有 5 列, 4 用于源 ip 八位字节,1 用于协议。
  • @user3648560 cmets 不是提出新问题的好地方。如果您现在遇到不同的问题,您可以在此网站上发布新帖子。
猜你喜欢
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多