Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error plotting SVM classification graph

Tags:

r

svm

I'm using the support vector machine from the e1071 package to classify my data and want to visualize how the machine actually does the classification. However, when using the plot.svm function, I get an error that I can't resolve.

Script:

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)

Output:

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:

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)

Part of my (4488 lines long) data file:

-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

Since I`m just starting with R, I have no idea what this means and how I should deal with it, nor did I find anything useful in other places.

like image 507
user655423 Avatar asked Feb 23 '10 10:02

user655423


1 Answers

Without being sure what exactly causes the problem, I would try to transform the Class column to a factor (so defining the type as C-classification will no longer be necessary) using something like this:

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

or in one step:

model <- svm(as.factor(Class)~.,data, kernel = "linear")
like image 133
George Dontas Avatar answered Nov 13 '22 23:11

George Dontas