R中SVM-RFE算法的实现

Posted

技术标签:

【中文标题】R中SVM-RFE算法的实现【英文标题】:Implementation of SVM-RFE Algorithm in R 【发布时间】:2016-12-17 22:23:49 【问题描述】:

我正在使用 R 代码来实现来自此源 http://www.uccor.edu.ar/paginas/seminarios/Software/SVM_RFE_R_implementation.pdf 的 SVM-RFE 算法,但我做了一个小的修改,以便 r 代码使用 gnum 库。代码如下:

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)
 

该函数接收matrix 作为inputxfactor 作为inputy。我对一些数据使用该函数,并且在最后一次迭代中收到以下错误消息:

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

调试代码,我得到了这个:

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) 

那么,函数的错误是什么?

【问题讨论】:

插入符号包似乎有 RFE:machinelearningmastery.com/… 【参考方案1】:

当只剩下一个特征时,看起来您的矩阵会转换为列表。试试这个:

svmModel = SVM(as.matrix(x[, survivingFeaturesIndexes]), y, C = 10, cache_size=500,kernel="linear" )

【讨论】:

以上是关于R中SVM-RFE算法的实现的主要内容,如果未能解决你的问题,请参考以下文章

多维关联规则挖掘算法r语言能实现吗

R语言如何实现K最近邻算法?

r SpamSumCompare - 原生R中SpamSum Hash比较算法的简化实现。

R中常用数据挖掘算法包

R语言:朴素贝叶斯算法实现对中文垃圾邮件的分类

R-CNN 系列 object detection 算法