r - 使用 R、支持向量机预测简单序列

标签 r dataframe machine-learning plot svm

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

输入 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) 的长度不等,并且它不是另一个的倍数,因此不会发生回收。一种选择是复制复制“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/

相关文章:

r - 如何重新排序几何图形的叠加顺序,但在 ggplot 中保持图例顺序完整

r - 在 modelsummary 的单独页面中导出多个表

r - 安装和加载包后找不到对象

r - 使用循环通过网络抓取创建表格

python - 在 Pandas 数据帧之间寻找最接近的值

python - Pandas - 根据第一列值删除行

matlab - 如何实现带有隐藏层的神经网络?

python - 删除具有给定子字符串值的行

machine-learning - 管道中 CountVectorizer 的 Sklearn NotFittedError

numpy - 处理具有数字属性的标称值的策略