How to do recursive feature elimination with SVM in R
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Recursive feature elimination, or RFE, repeatedly fits a model, ranks features, removes the weakest ones, and evaluates the smaller feature sets. In R, a practical way to do this with an SVM is to use caret::rfe together with a linear SVM model so the ranking step is well defined.
Why Linear SVM Is the Usual Starting Point
RFE needs some notion of feature importance so it can decide what to remove next. With SVMs, that is simplest when you use a linear kernel, because the model has coefficients that can support ranking.
That is why most RFE-with-SVM examples start with a linear SVM rather than a non-linear kernel.
A Runnable caret Example
The caret package provides an rfe function that can repeatedly train and evaluate candidate feature subsets.
This code does several important things:
- uses cross-validation during the feature-selection process
- evaluates several candidate subset sizes
- trains a linear SVM for each subset
- reports the chosen feature set and its resampling performance
How to Read the Result
The print output summarizes performance for each subset size and reports the selected number of predictors. predictors(svm_profile) returns the final chosen feature names.
The plot is also useful because it shows whether model performance improves, plateaus, or degrades as features are removed.
That matters because RFE is not about finding the smallest possible set. It is about finding a smaller set that still predicts well.
Training a Final Model on the Selected Features
Once RFE chooses a subset, you can train a final SVM using only those predictors.
This makes the modeling pipeline explicit: first select features, then train the final model on that selected subset.
Why Preprocessing Matters
SVMs are sensitive to feature scale, so centering and scaling are usually important. In caret, pass preProcess = c("center", "scale") so the resampling workflow applies preprocessing consistently.
Do not scale the full dataset outside the resampling process and then run RFE on the already-transformed data. That can leak information across folds.
What About Non-Linear SVMs
You can fit non-linear SVMs in R, but feature ranking becomes less straightforward because there is no simple coefficient vector analogous to the linear case.
That does not make non-linear models bad. It just means linear SVM is the natural starting point when the goal is coefficient-driven recursive elimination.
Common Pitfalls
A common mistake is using RFE with an SVM method that does not provide a clean ranking signal and then assuming the selected features are trustworthy.
Another mistake is forgetting to scale predictors. SVMs can behave poorly when one feature dominates only because of its numeric range.
A third issue is interpreting the chosen feature subset as universally optimal. RFE results depend on the data, resampling strategy, and model settings.
Summary
- In R,
caret::rfeis a practical way to run recursive feature elimination with SVM - A linear SVM is the usual choice because feature ranking is clearer
- Use cross-validation inside the RFE workflow, not just after it
- Center and scale predictors during the resampling process
- After selection, train a final model using the chosen feature subset
Related reading
- How to do slice assignment in Tensorflow
- How to do the group-by operation in Tensorflow?
- How to do transfer learning for MNIST dataset?
- How to do transfer learning for MNIST dataset?
- How to download graphs from tensorboard?
- How to draw vertical lines on a given plot
- How to do Xavier initialization on TensorFlow
- How to download datasets for sklearn? - python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.