Implementation of SVM-RFE Algorithm in R

2019-07-29 16:43发布

I'm using the R code for the implementation of SVM-RFE Algorithm from this source http://www.uccor.edu.ar/paginas/seminarios/Software/SVM_RFE_R_implementation.pdf but I made a small modification so that the r code uses the gnum library. The code is the following:

svmrfeFeatureRanking = function(x,y){
  n = ncol(x)

  survivingFeaturesIndexes = seq(1:n)
  featureRankedList = vector(length=n)
  rankedFeatureIndex = n

  while(length(survivingFeaturesIndexes)>0){
    #train the support vector machine
    svmModel = SVM(x[, survivingFeaturesIndexes], y, C = 10, cache_size=500,kernel="linear" )



    #compute ranking criteria
    rankingCriteria = svmModel$w * svmModel$w

    #rank the features
    ranking = sort(rankingCriteria, index.return = TRUE)$ix

    #update feature ranked list
    featureRankedList[rankedFeatureIndex] = survivingFeaturesIndexes[ranking[1]]
    rankedFeatureIndex = rankedFeatureIndex - 1

    #eliminate the feature with smallest ranking criterion
    (survivingFeaturesIndexes = survivingFeaturesIndexes[-ranking[1]])

  }

  return (featureRankedList)
} 

That function receive a matrix as an input for x and a factor as an input for y. I use the function for some data , and I receive the following error message in the last iterations:

 Error in if (nrow(x) != length(y)) { : argument is of length zero 

Debugging the code, I got this:

3 SVM.default(x[, survivingFeaturesIndexes], y, C = 10, cache_size = 500, 
    kernel = "linear") 
2 SVM(x[, survivingFeaturesIndexes], y, C = 10, cache_size = 500, 
    kernel = "linear") 
1 svmrfeFeatureRanking(sdatx, ym) 

So, what's the error of the function?

1条回答
老娘就宠你
2楼-- · 2019-07-29 17:33

Looks like your matrix gets converted into a list when only one feature remains. Try this:

svmModel = SVM(as.matrix(x[, survivingFeaturesIndexes]), y, C = 10, cache_size=500,kernel="linear" )
查看更多
登录 后发表回答