Skip to content

Instantly share code, notes, and snippets.

@gangchen
Created November 27, 2012 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangchen/4152610 to your computer and use it in GitHub Desktop.
Save gangchen/4152610 to your computer and use it in GitHub Desktop.
R implementation of SVM-RFE from literature
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, cost = 10, cachesize=500,scale=F, type="C-classification", kernel="linear" )
#compute the weight vector
w = t(svmModel$coefs)%*%svmModel$SV
#compute ranking criteria
rankingCriteria = w * 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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment