gpt4 book ai didi

r - 1 :nrow(newdata) : argument of length 0 while using SVM predict 中出现错误

转载 作者:行者123 更新时间:2023-11-30 09:53:31 26 4
gpt4 key购买 nike

我正在尝试根据 svm 训练的模型来预测 A 值。这就是我的训练和测试数据的样子:

A     B    C     D
r00 r01 r02 r03
... ... ... ...

代码片段如下:

featvecs = ["B"]

for (f in 1:nrow(featvecs)) {
tuned <- svm(A ~., data = train[,c("A",featvecs[f,])], gamma = 0.01, cost = 10, kernel= "radial")
svm.predict <- predict(tuned, test[,featvecs[f,]])
}

我在 svm.predict 行中收到以下错误,但不太确定原因?

Error in 1:nrow(newdata) : argument of length 0

列车数据的结构:

structure(list(A = structure(6L, .Label = c("'1'", 
"'2'", "'3'" ), class = "factor"), B = structure(15L, .Label = c(...)...)

测试数据的结构:

structure(list(A = structure(2L, .Label = c("'1'", 
"'2'", "'3'" ), class = "factor"), B = structure(17L, .Label = c(...)...)

最佳答案

我怀疑 featvecs 只有一列,因此 featvecs[f,] 的长度为 1

然后test[,featvecs[f,]]输出一个向量而不是预期的data.frame(参见mtcars[, "mpg"]mtcars[, "mpg", drop = FALSE]) 和 nrow() 应用于向量输出 NULL: 1: svm.predict() 源代码中的 nrow(newdata) 给出了导致错误的 1:NULL

尝试将 drop = FALSE 添加到 test[,featvecs[f,], drop = FALSE] 以便获得 data.frame。

关于r - 1 :nrow(newdata) : argument of length 0 while using SVM predict 中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40013817/

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