0
votes

I am working on pso optimisation technique to optimize SVM parameters gamma and the C. I got this error and I couldn't figure out it.

Error in dimnames(x) <- dn:

length of 'dimnames' [2] not equal to array extent

Called from: colnames <- (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)
1

1 Answers

0
votes

Try changing the following lines of code from

  • predictedY <- predict(svm.model, x) to predictedY <- predict(svm.model, tes) and
  • pred <- predict(svm.model, x, decision.values = TRUE) to pred = predict(svm.model, tes, decision.values = TRUE)