我使用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
input
x
{ - {}} factor
input
y
。我将函数用于某些数据,并在最后一次迭代中收到以下错误消息:
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)
那么,该函数的错误是什么?
答案 0 :(得分:1)
当只剩下一个要素时,看起来您的矩阵会转换为列表。试试这个:
svmModel = SVM(as.matrix(x[, survivingFeaturesIndexes]), y, C = 10, cache_size=500,kernel="linear" )