[英]Cannot train KSVM in R
我整天都在這。 假設我有一個如下的訓練數據
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
我用Google搜索了,但我無法想出解決方案。
我做錯了什么,我怎么能讓它發揮作用!?
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")
我遇到了同樣的問題,問題實際上是我的數據集中存在一些無限元素。
您可以使用諸如apply(xtrain,2,range)之類的東西來檢查它們,然后使用xtrain $ your_var [xtrain $ your_var == Inf] < - 0將它們任意設置為零。
在我的情況下,問題是NA值。 我刪除了它們,我想通了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.