Implementation of SVM-RFE Algorithm in R

二次信任 提交于 2020-01-15 11:07:16

问题


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:


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" )


来源:https://stackoverflow.com/questions/38891965/implementation-of-svm-rfe-algorithm-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!