Dino Fire
Dino Fire

Reputation: 419

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

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.

Upvotes: 2

Views: 14935

Answers (1)

Abhinav Pathak
Abhinav Pathak

Reputation: 108

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)

Upvotes: 5

Related Questions