绘制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,我不知道这意味着什么,应该如何处理,也没有在其他地方找到任何有用的东西。
如果不确定究竟是什么导致了问题,我会尝试使用类似下面的方法将Class
列转换为一个因子(因此将类型定义为不再需要C-classification
):
data$Class <- as.factor(data$Class)
或者在一个步骤中:
model <- svm(as.factor(Class)~.,data, kernel = "linear")
链接地址: http://www.djcxy.com/p/45001.html