Testing svm错误测试数据与模型不匹配?

Testing svm错误测试数据与模型不匹配?,testing,svm,prediction,training-data,Testing,Svm,Prediction,Training Data,我试图训练一个svm分类器来做预测。当我尝试使用经过训练的模型时,我得到了这个错误:测试数据与模型不匹配。我不明白为什么会这样。这是我的密码 # to prepare the training and testing data dat = data.frame(x = rbind(tmp1, tmp2), y = as.factor(c(rep(1, 300), rep(-1, 300)))) set.seed(1) train_ind = sample(seq_len(nrow(dat)),

我试图训练一个svm分类器来做预测。当我尝试使用经过训练的模型时,我得到了这个错误:测试数据与模型不匹配。我不明白为什么会这样。这是我的密码

# to prepare the training and testing data
dat = data.frame(x = rbind(tmp1, tmp2), y = as.factor(c(rep(1, 300), rep(-1, 300))))
set.seed(1)
train_ind = sample(seq_len(nrow(dat)), size = 500)
train = dat[train_ind, ]
test = dat[-train_ind, ]

# training and prediction
library('e1071')
svmfit = svm(y ~ ., data = train, kernel ='linear', cost = 10, scale = FALSE)
ypred = predict(svmfit, test)
table(predict=ypred, truth = test$y)

这个错误背后的原因是我在训练和测试数据中包含了观察的ID,这混淆了svm分类器。观察的ID在第一列中。因此,当我从培训和测试中删除第一列时,它起了作用。

如果您的培训数据集中有一个分类预测变量(自变量),那么只有培训数据集中的类别才能出现在测试数据集中。如果是这种情况,请检查测试数据集中的所有类别是否都存在于培训数据集中。有些时候,SVM假设一个整数变量是分类变量,其范围很短,比如用数字表示的月份[1:12]