library(e1071)
library(caret)
library(kernlab)
data <- read.table(file.choose(), header = T, sep=";", quote="")
s <-sample(213,149)
tra <- data[s,]
tes<- data[-s,]
x <- subset(tes, select = -Cardio)
y <- tes$Cardio
# objective function
f <- function(y,data,C_exp, gamma_exp)
{
set.seed(123)
C = 10 ** C_exp
gamma = 10 ** gamma_exp
clf = svm.SVC(C=C, gamma=gamma)
wts <- 100 / table(data$Cardio)
svm.model <- svm(Cardio ~ ., data = tra, scale = FALSE,kernel="radial",gamma=gamma,cost=0.01,class.weights = wts)
predictedY <- predict(svm.model, x)
error <- data$Cardio - predictedY
# Check accuracy:
table( predictedY, y)
# compute decision values and probabilities:
pred <- predict(svm.model, x, decision.values = TRUE)
# attr(pred, "decision.values")[1:4,]
mean(pred==tes[,72])
return (RMSE(error))
}
n <- 50
m.l <- 50
w <- 0.95
c1 <- 0.2
c2 <- 0.2
xmin <- c(-5.12, -5.12)
xmax <- c(5.12, 5.12)
vmax <- c(4, 4)
optimum <-psoptim(f, n=n, max.loop=m.l, w=w, c1=c1, c2=c2,xmin=xmin, xmax=xmax, vmax=vmax, seed=5, anim=FALSE)
我正在尝试用PSO技术优化svm参数,但我仍然感到困惑,因为我收到了这个错误
FUN(x)出错:缺少参数“C_exp”,没有默认值
请提前帮助并谢谢