我正在研究pso优化技术来优化SVM参数gamma和C.我得到了这个错误,我无法弄明白。
dimnames(x) <- dn
中的错误:
&#39> dimnames的长度&#39; [2]不等于数组范围来自:colnames&lt; - ( tmp ,value = colnames(object $ SV)
library(e1071)
library(caret)
library(kernlab)
library(psoptim)
#plot(data, pch=16)//margins too large
s <-sample(213,149)
tra <- data[s,]
tes<- data[-s,]
x <- subset(tes, select = -Cardio)
y <- tes$Cardio
#error rate function
error_rate <- function(yobs,ypred){
#matrice de confusion
mc <- table(yobs,ypred)
#taux d'erreur
err <- 1.0 - sum(diag(mc))/sum(mc)
return(err)
}
# objective function minimization
f <- function(x)
{
set.seed(123)
C <- x[0]
gamma <- x[0]
# C <- 10 ** C_Exp
# gamma <- 10 ** gamma_Exp
wts <- 100 / table(data$Cardio)
svm.model <- svm(Cardio ~ ., data = tra, scale = FALSE,type="C-classification",kernel="radial",gamma=gamma,cost=C,class.weights = wts)
predictedY <- predict(svm.model, x)
#fonction d'évaluation
#prediction error
error <- error_rate(tes$Cardio,predictedY)
# compute decision values and probabilities:
pred <- predict(svm.model, x, decision.values = TRUE)
# attr(pred, "decision.values")[1:4,]
# Check accuracy:
mean(pred==tes[,72])
#results of prediction
table((tes[,72])== pred)
return (error)
}
n <- 100 #population size
m.l <- 100 #number of epochs
w <- 0.95 #inertia weight
c1 <- 0.2#accelaration factor self-recognition
c2 <- 0.2 #acceleration factor social component
xmin <- c(-5.12, -5.12) #position minimale
xmax <- c(5.12, 5.12) #position maximale
vmax <- c(4, 4) #velocity
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)
答案 0 :(得分:0)
尝试更改以下代码行