我正在尝试使用支持向量机来预测值序列,例如:
输入 0, 1, 2, 3 将预测 4
出于这个原因,我将这个问题作为 R 中的回归 ML 问题来处理,这是我的代码:
library("e1071")
x0 <- c(0)
x1 <- c(0, 1)
x2 <- c(0, 1, 2)
x3 <- c(0, 1, 2, 3)
x4 <- c(0, 1, 2, 3, 4)
x5 <- c(0, 1, 2, 3, 4, 5)
x6 <- c(0, 1, 2, 3, 4, 5, 6)
x7 <- c(0, 1, 2, 3, 4, 5, 6, 7)
x = c(x0, x1, x2, x3, x4, x5, x6, x7)
y = c(1, 2, 3, 4, 5, 6, 7, 8)
df = data.frame(x, y)
df
svmfit = svm(y ~ ., data = df)
print(svmfit)
目前,我陷入了如何正确创建输入序列的困境,并且不断收到此错误:
Error in data.frame(x, y): arguments imply differing number of rows: 36, 8 Traceback:
- data.frame(x, y)
- stop(gettextf("arguments imply differing number of rows: %s", . paste(unique(nrows), collapse = ", ")), domain = NA)
有人可以帮助我吗?
提前非常感谢!
最佳答案
问题在于“x”、“y”向量 (36, 8) 的长度不等,并且它不是另一个的倍数,因此不会发生回收。一种选择是复制
复制“y”向量以使长度与“x”相同,然后执行svm
df <- data.frame(x, y = rep(y, length.out = length(x)))
svmfit = svm(y ~ ., data = df)
svmfit
#Call:
#svm(formula = y ~ ., data = df)
#Parameters:
# SVM-Type: eps-regression
# SVM-Kernel: radial
# cost: 1
# gamma: 1
# epsilon: 0.1
#Number of Support Vectors: 34
关于r - 使用 R、支持向量机预测简单序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55972412/