【发布时间】:2015-08-29 09:58:33
【问题描述】:
我整天都在做这件事。假设我有如下训练数据
1.0000000 0.8260869 0
0.7333333 0.4666667 0
0.0000000 0.0000000 0
0.3076923 0.3076923 0
0.2307692 0.4615385 0
0.9333333 0.4666667 1
0.3157895 0.4210526 1
1.0000000 0.7000000 1
0.3157895 0.2631579 1
0.6666667 0.4444444 1
前几列是我们的特征集,每行的最后一列是我们尝试学习/预测的标签。
但是当我尝试使用以下脚本为上述数据训练 SVM 时,我编写了以下脚本:
library(kernlab)
library(Matrix)
kp = function(d, e){
gama = 0.25
DA = d[,1]
DB = d[,2]
DE = e[,1]
DF = e[,2]
q1 = (norm(as.matrix(DA-DE)))^2
q2 = (norm(as.matrix(DB-DF)))^2
q3 = (norm(as.matrix(DA-DF)))^2
q4 = (norm(as.matrix(DB-DE)))^2
s1 = min((q1+q2),(q3+q4))
s = (norm(as.matrix(s1)))^2
exp(-gama*s)
}
data <- read.csv(file = "dataset.dat", stringsAsFactors = TRUE, nrows = 10)
xtrain <- as.matrix(data[,1:2])
ytrain <- as.matrix(data[,687])
class(kp)<-"kernel"
ksvm(x = xtrain, y = ytrain, type = "C-svc", kernel = kp, C = 128, scale = FALSE)
我收到以下错误
Error in indexes[[j]] : subscript out of bounds
Calls: ksvm -> ksvm -> .local
Execution halted
我已经用谷歌搜索过了,但我找不到解决方案。
问题:
我做错了什么,我该如何让它发挥作用!?
编辑
traceback()的结果如下:
3: .local(x, ...)
2: ksvm(x = xtrain, y = ytrain, type = "C-svc", kernel = kp, C = 128)
1: ksvm(x = xtrain, y = ytrain, type = "C-svc", kernel = kp, C = 128)
还有dput(data)
structure(list(X0.8 = c(1, 0.7333333, 0, 0.3076923, 0.2307692), X0.7 = c(0.8260869, 0.4666667, 0, 0.3076923, .4615385)), .Names = c("X0.8", "X0.7"), row.names = c(NA, 5L), class = "data.frame")
【问题讨论】:
-
我相信你的问题可能在于训练数据输入
x和响应向量y。你能检查一下y是否应该是一个矩阵? -
发布
traceback()的结果。理想情况下,一个小数据集的dput()可以重现问题。 -
问题已被编辑。