Error in predict.svm: test data does not match model

非 Y 不嫁゛ 提交于 2019-12-01 07:49:57

问题


I have a data frame of about 500 rows and 170 columns. I am attempting to run a classification model with svm from the e1071 package. The classification variable is called 'SEGMENT', a factor variable with 6 levels. There are three other factor variables in the data frame, and the rest are numeric.

data <- my.data.frame
# Split into training and testing sets, training.data and testing.data
.
.
.
fit <- svm(SEGMENT ~ ., data = training.data, cost = 1, kernel = 'linear', 
+ probability = T, type = 'C-classification')

The model runs fine.

Parameters:
SVM-Type:  C-classification 
SVM-Kernel:  linear 
   cost:  1 
   gamma:  0.0016 

Number of Support Vectors:  77

( 43 2 19 2 2 9 )

Number of Classes:  6 

Levels: 
EE JJ LL RR SS WW

The problem arises when I try to test the model on data.testing, which is structured exactly like the training set:

x <- predict(fit, testing.data, decision.values = T, probability = T)

And then things blow up rather spectacularly:

Error in predict.svm(fit, newdata = testing, decision.values = T, probability = T) : 
test data does not match model !

Ideas are most welcome.


回答1:


This happens when the columns in test and train data aren't same. Try str(training.data) & str(testing.data) they should have the same variables except for the one that needs to be predicted. Include only those factors you want to use for prediction in the svm training model.

For eg:

fit <- svm(SEGMENT ~ ., data = training.data[,1:6], cost = 1, kernel = 'linear', 
+ probability = T, type = 'C-classification')     


x <- predict(fit, testing.data[,1:5], decision.values = T, probability = T)


来源:https://stackoverflow.com/questions/26265305/error-in-predict-svm-test-data-does-not-match-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!