小编Dai*_*oga的帖子

如何使用R的遗传算法优化支持向量机的参数

为了学习支持向量机,我们必须确定各种参数。

例如,有成本和伽马等参数。

我正在尝试使用 R 的“GA”包和“kernlab”包来确定 SVM 的 sigma 和 gamma 参数。

我使用accuracy作为遗传算法的评价函数。

我创建了以下代码,并运行了它。

library(GA) 
library(kernlab) 
data(spam) 
index <- sample(1:dim(spam)[1]) 
spamtrain <- spam[index[1:floor(dim(spam)[1]/2)], ] 
spamtest <- spam[index[((ceiling(dim(spam)[1]/2)) + 1):dim(spam)[1]], ] 

f <- function(x) 
{ 
x1 <- x[1] 
x2 <- x[2] 
filter <- ksvm(type~.,data=spamtrain,kernel="rbfdot",kpar=list(sigma=x1),C=x2,cross=3) 
mailtype <- predict(filter,spamtest[,-58]) 
t <- table(mailtype,spamtest[,58]) 
return(t[1,1]+t[2,2])/(t[1,1]+t[1,2]+t[2,1]+t[2,2]) 
} 

GA <- ga(type = "real-valued", fitness = f, min = c(-5.12, -5.12), max = c(5.12, 5.12), popSize = 50, maxiter = 2) 
summary(GA) 
plot(GA) 
Run Code Online (Sandbox Code Playgroud)

但是,当我调用 GA 函数时,返回以下错误。

“未找到支持向量。您可能需要更改参数”

我不明白为什么代码不好。

r classification machine-learning svm

3
推荐指数
1
解决办法
7734
查看次数

标签 统计

classification ×1

machine-learning ×1

r ×1

svm ×1