我正在使用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)
那么,该函数有什么错误呢?
最佳答案
当仅保留一个功能时,矩阵似乎转换为列表。尝试这个:
svmModel = SVM(as.matrix(x[, survivingFeaturesIndexes]), y, C = 10, cache_size=500,kernel="linear" )