2010-02-23 86 views
7

我正在使用e1071軟件包中的支持向量機對我的數據進行分類,並希望可視化機器實際進行分類的方式。但是,使用plot.svm函數時,出現無法解析的錯誤。繪製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 

回溯:我(4488線長)數據文件的

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) 

部分:

-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,我不知道這意味着什麼,我該如何處理它,也沒有在其他地方找到任何有用的東西。

回答

4

沒有被確定到底是什麼導致了問題,我會用這樣的嘗試將Class列轉換爲一個因子(所以在定義類型爲C-classification將不再是必要的):

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

或在一步:

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

謝謝你的傢伙。奇蹟般有效。就像你建議的那樣,我將Class列轉換爲一個因子,並在svm的調用中刪除了'type'參數。錯誤消失了。 – user655423 2010-02-23 12:53:36