绘制SVM分类图时出错

时间:2010-02-23 10:52:00

标签: r svm

我正在使用e1071软件包中的支持向量机对我的数据进行分类,并希望可视化机器实际进行分类的方式。但是,当使用plot.svm函数时,我收到一个无法解决的错误。

脚本:

library("e1071")

data <-read.table("2010223_11042_complete")
names(data) <- c("Class","V1", "V2")

model <- svm(Class~.,data, type = "C-classification", kernel = "linear")
plot(model,data,fill=TRUE, grid=200, svSymbol=4, dataSymbol=1, color.palette=terrain.colors)

输出:

plot(model,data,fill=TRUE, grid=200, svSymbol=4, dataSymbol=1, color.palette=terrain.colors)
Error in rect(0, levels[-length(levels)], 1, levels[-1L], col = col) : 
  cannot mix zero-length and non-zero-length coordinates

回溯:

traceback()
4: rect(0, levels[-length(levels)], 1, levels[-1L], col = col)
3: filled.contour(xr, yr, matrix(as.numeric(preds), nr = length(xr), 
       byrow = TRUE), plot.axes = {
       axis(1)
       axis(2)
       colind <- as.numeric(model.response(model.frame(x, data)))
       dat1 <- data[-x$index, ]
       dat2 <- data[x$index, ]
       coltmp1 <- symbolPalette[colind[-x$index]]
       coltmp2 <- symbolPalette[colind[x$index]]
       points(formula, data = dat1, pch = dataSymbol, col = coltmp1)
       points(formula, data = dat2, pch = svSymbol, col = coltmp2)
   }, levels = 1:(length(levels(preds)) + 1), key.axes = axis(4, 
       1:(length(levels(preds))) + 0.5, labels = levels(preds), 
       las = 3), plot.title = title(main = "SVM classification plot", 
       xlab = names(lis)[2], ylab = names(lis)[1]), ...)
2: plot.svm(model, data, fill = TRUE, grid = 200, svSymbol = 4, 
       dataSymbol = 1, color.palette = terrain.colors)
1: plot(model, data, fill = TRUE, grid = 200, svSymbol = 4, 
       dataSymbol = 1, color.palette = terrain.colors)

我的(4488行)数据文件的一部分:

-1 0 23.532
+1 1 61.1157
+1 1 61.1157
+1 1 61.1157
-1 1 179.03
-1 0 17.0865
-1 0 27.6201
-1 0 17.0865
-1 0 27.6201
-1 1 89.6398
-1 0 42.7418
-1 1 89.6398

由于我只是从R开始,我不知道这意味着什么以及我应该如何处理它,我也没有在其他地方找到任何有用的东西。

1 个答案:

答案 0 :(得分:4)

我不确定究竟是什么导致了这个问题,我会尝试将Class列转换为一个因子(因此不再需要将类型定义为C-classification),使用以下内容:< / p>

data$Class <- as.factor(data$Class)

或一步到位:

model <- svm(as.factor(Class)~.,data, kernel = "linear")