Created
November 27, 2012 05:56
-
-
Save gangchen/4152610 to your computer and use it in GitHub Desktop.
R implementation of SVM-RFE from literature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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