利用遗传算法结合R
为了学习支持向量机,我们必须确定各种参数 例如,存在成本和伽马等参数 我尝试使用R的“GA”包和“kernlab”包来确定SVM的sigma和gamma参数 我使用精度作为遗传算法的评价函数 我创建了以下代码,并运行了它利用遗传算法结合R,r,machine-learning,classification,svm,R,Machine Learning,Classification,Svm,为了学习支持向量机,我们必须确定各种参数 例如,存在成本和伽马等参数 我尝试使用R的“GA”包和“kernlab”包来确定SVM的sigma和gamma参数 我使用精度作为遗传算法的评价函数 我创建了以下代码,并运行了它 library(GA) library(kernlab) data(spam) index <- sample(1:dim(spam)[1]) spamtrain <- spam[index[1:floor(dim(spam)[1]/2)], ] spam
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)
库(GA)
图书馆(内核实验室)
数据(垃圾邮件)
索引对SVM参数使用GA不是一个好主意-只需进行常规网格搜索就足够了(两个用于循环,一个用于C
和一个用于gamma
值)
在Rs库e1071(也提供支持向量机)中,有一种方法
tune.svm`使用网格搜索查找最佳参数
示例
data(iris)
obj <- tune.svm(Species~., data = iris, sampling = "fix",
gamma = 2^c(-8,-4,0,4), cost = 2^c(-8,-4,-2,0))
plot(obj, transform.x = log2, transform.y = log2)
plot(obj, type = "perspective", theta = 120, phi = 45)
数据(iris)
obj如果你只有两个参数需要优化,你并不需要GA。定义一个合理值的网格,并测试你的模型在网格上的拟合度。你好,Hong Ooi!谢谢你马上回复。上面的代码是一个特别的示例。我计划最终使用我自己定制的svm内核。我计划为svm使用五到六个参数。如果我使用网格搜索,组合的数量是巨大的。所以,我想用遗传算法来优化参数。你确定sigma
和C
的负值有意义吗?嘿,文森特·佐内基德!谢谢你的回复。我很抱歉。上述值设置不完整。我只给出了一个代码示例。我将为实际分析指定精确的值。@VincentZoonekynd我确信它们不是。