gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:37:41 30 4
gpt4 key购买 nike

我正在尝试训练支持向量机分类器来进行预测。当我尝试使用经过训练的模型时,出现此错误:测试数据与模型不匹配。我不是为什么会这样。这是我的代码

# 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 在第一列中。因此,当我从训练和测试中删除第一列时,它起作用了。

关于testing - svm错误测试数据与模型不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48040491/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com