用 R 支持向量机预测一个简单的序列

Predict a simple sequence with R, support vector machine

我正在尝试使用支持向量机来预测一系列值,例如:

输入 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:

  1. data.frame(x, y)
  2. stop(gettextf("arguments imply differing number of rows: %s", . paste(unique(nrows), collapse = ", ")), domain = NA)

谁能帮帮我?

非常感谢!

问题在于 'x'、'y' 向量 (36, 8) 的长度不等,并且不是另一个的倍数,因此不会发生回收。一种选择是 replicate '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