Feature Selection in ML

|
14 min read
|
29 views
Feature Selection in ML

Feature selection changes the accuracy of a model less than the order in which it is applied. A model trained on features chosen before the train/test split reports higher accuracy than the same model tested honestly. The difference between the two figures is what the article is all about.

Feature selection is the process of choosing which input variables a machine learning model used for training, based on their relevance to the target variable. All other features are removed before training starts.

The article explores three types of feature selection techniques, when you should use one over the other, the leakage mistake that results in inflated accuracies, when feature selection is useless, and includes a working Python code example.

What Is Feature Selection?

Feature selection refers to the task of picking a subset of important input features from the data set, while eliminating those that may cause noise or redundancy in the modeling.

Feature selection represents one of the stages of feature engineering. Feature engineering refers to all the actions that need to be taken when preparing data for modeling. These actions include the creation, transformation, and selection of new features. Feature selection concerns itself only with the action of selection.

Feature selection and feature extraction should not be confused. While feature selection involves retaining a subset of the original features, feature extraction involves creating new features through transformations or combinations of the existing features. Principal Component Analysis (PCA) is a feature extraction technique and not a feature selection technique since PCA creates new components and does not retain any of the original columns.

Professional Certificate

Machine Learning Course

Learn supervised, unsupervised and ensemble ML techniques with Python — from model building to real-world deployment.

4.7 (5,874 ratings) • 13,510 already enrolled • Beginner level

Class Starts on 20 Sep, 2026 — SAT & SUN (Weekend Batch)

Average time: 5 month(s)

Skills you’ll build: Python, Scikit-learn, Supervised & Unsupervised Learning, Feature Engineering, Model Deployment, and more..

What Counts as a Feature?

A feature is an attribute of data points used to construct models. Examples of features in a dataset of housing are square footage, number of rooms, and year built. The classification of features is done through:

Feature typeDefinitionExample
NumericalA feature with a quantifiable, ordered valueAge, price, square footage
CategoricalA feature with a non-numerical, unordered valueCity, job title, product category

The target variable is the output the model is trained to predict. Feature selection identifies which input features have the strongest relationship with the target variable.

Why Feature Selection Matters

Feature selection produces five measurable effects on a model. Each effect is a trade, not a guaranteed gain.

EffectMechanismCondition for benefit
Higher accuracyRemoves variables that add noise to the training signalOnly when removed variables were genuinely uninformative
Lower overfittingFewer parameters reduce the model’s capacity to memorize training dataMost useful on high-dimensional, low-sample datasets
Shorter training timeFewer input columns reduce computation per training stepScales with the number of features removed
Lower inference costFewer features reduce the data pipeline required at prediction timeMatters most in production systems with per-feature retrieval cost
Greater interpretabilityFewer variables make model behavior easier to traceMost relevant for regulated or audited use cases

Feature selection is not an assurance of accuracy improvement. Deleting any feature containing genuine information decreases the accuracy. It all depends on being able to identify the features that do not contain useful information.

The Curse of Dimensionality

The Curse of Dimensionality is a phenomenon of decline in model performance due to an increasing ratio of input dimensions to the number of training samples, as data points become more sparse in higher-dimensional space.

As we move into more dimensions, the feature space volume increases exponentially while the number of data points remains constant. For instance, the data set of 100 observations with 5 dimensions has data points at an interval of approximately 0.4 on each dimension according to the usual distance formula 1/N^1/d where N is the total number of observations and d is the number of dimensions. Similarly, the same number of data points with 50 features has almost empty feature space, as the interval between each data point comes down to an approximate distance of 0.09 on each dimension. 

Feature Selection vs. Feature Extraction vs. Dimensionality Reduction

PropertyFeature selectionFeature extractionDimensionality reduction
OutputA subset of original featuresNew, transformed featuresEither selection or extraction
Modifies original valuesNoYesDepends on method
InterpretabilityHigh — original features remain identifiableLower — new features are combinationsDepends on method
Example methodsFilter, wrapper, embedded methodsPCA, Linear Discriminant Analysis (LDA), autoencodersIncludes both categories above
RelationshipA category of dimensionality reductionA category of dimensionality reductionThe umbrella term for both categories

Feature selection is a subclass of dimensionality reduction. Feature extraction is another subclass. Dimensionality reduction refers to any technique that reduces the number of input dimensions without consideration for whether the original feature space is preserved or altered.

The Three Types of Feature Selection Methods

Feature selection methods are grouped into three categories based on how they interact with the machine learning model: filter, wrapper, and embedded methods.

Method typeHow it worksUses the model during selectionRelative speedBest suited for
FilterScores each feature using a statistical test against the target variableNoFastLarge datasets, initial screening
WrapperTrains and evaluates the model on different feature subsetsYesSlowSmall to mid-sized feature spaces
EmbeddedSelects features as part of the model’s own training processYes, intrinsicallyModerateRegularized linear models, tree-based models

Filter Methods

Filter approaches involve the selection of features based on their score obtained individually using statistical measures for their relevance to the target variable; no machine learning model is trained. Because filter approaches do not rely on any particular model, the resulting ranking holds for any model.

TechniqueStatistical basisApplies to
Information gainReduction in entropy when a feature is knownCategorical target
Mutual informationShared information between two variablesNumerical or categorical
Chi-square testAssociation between two categorical variablesCategorical input and target
Fisher’s scoreClass separability by featureClassification tasks
Pearson’s correlation coefficientLinear relationship, ranging from -1 to 1Continuous input and target
Variance thresholdRemoves features below a set variance valueAny numerical feature
Missing value ratioPercentage of missing entries per featureAny feature type
ANOVAWhether group means differ significantlyCategorical input, numerical target

Filter approaches cannot eliminate multicollinearity. In cases where two variables have high scores on the target feature, they may contain identical information. There is a need to apply an additional approach in order to eliminate correlated variables.

Wrapper Methods

Wrapper methods train models on various sets of features and retain the one that gives the highest validation accuracy. The wrapper method evaluates the feature set in terms of its accuracy, while filter methods do not.

TechniqueProcessStopping condition
Forward selectionStarts with zero features, adds one at a timeStops when adding a feature no longer improves performance
Backward eliminationStarts with all features, removes one at a timeStops when removing a feature reduces performance
Recursive Feature Elimination (RFE)Trains the model, ranks features by importance, removes the lowest-ranked feature, repeatsStops at a preset number of features
RFE with cross-validation (RFECV)Runs RFE across multiple validation foldsStops at the feature count with the highest average cross-validated score
Exhaustive selectionTests every possible feature combinationStops after all combinations are scored

Exhaustive selection checks all the 2ⁿ − 1 possible combinations of n features. If there are 20 features, the number of combinations would be 1,048,575. The above process becomes too costly to use beyond small numbers of features, and forward, backward, and recursive selections become common methods.

The Boruta algorithm is a wrapper algorithm that works on the basis of feature importance, in which it compares every feature with its randomized version(shadow feature). A feature is selected if it is better than its shadow feature.

Embedded Methods

The embedded method is one where the selection of features occurs during the training of the model, based on the optimization mechanism of the model itself.

TechniqueMechanismOutput
LASSO regression (L1 regularization)Adds a penalty proportional to the absolute value of each coefficientCoefficients of irrelevant features shrink to exactly zero
Elastic netCombines L1 and L2 penaltiesSelects features while retaining groups of correlated ones
Random forest importanceMeasures the average reduction in impurity a feature produces across all treesA ranked importance score per feature
Gradient boosting importanceMeasures the reduction in prediction error a feature produces across boosting iterationsA ranked importance score per feature

The ridge regression technique (L2 regularization) is not a feature selection approach. L2 regularization makes the model coefficients smaller without setting them to zero; thus, all of the features are included in the model. The only way that we can obtain coefficients equal to zero and thus perform feature selection is through L1 regularization.

Professional Certificate

Machine Learning Course

Learn supervised, unsupervised and ensemble ML techniques with Python — from model building to real-world deployment.

4.7 (5,874 ratings) • 13,510 already enrolled • Beginner level

Class Starts on 20 Sep, 2026 — SAT & SUN (Weekend Batch)

Average time: 5 month(s)

Skills you’ll build: Python, Scikit-learn, Supervised & Unsupervised Learning, Feature Engineering, Model Deployment, and more..

Hybrid and Unsupervised Approaches

Hybrid approaches use a filtering phase, which filters out irrelevant features, followed by a wrapping or embedded phase to select the subset from the filtered feature pool, thus cutting down the cost associated with performing a wrapper approach on all the features.

Unsupervised feature selection is applied where there is no target variable. PCA, ICA, and autoencoders are employed in this case. The three approaches mentioned above are feature extraction methods, not feature selection methods, since they generate new features and do not select a subset of the initial features.

How to Choose a Feature Selection Method

The correct method depends on the data type of the input and output variables.

Input typeOutput typeProblem typeRecommended method
NumericalNumericalRegressionPearson’s correlation coefficient
NumericalCategoricalClassificationANOVA (linear), Kendall’s rank correlation (nonlinear)
CategoricalNumericalRegressionCorrelation methods that support categorical variables
CategoricalCategoricalClassificationChi-square test, information gain

Three additional factors determine method choice:

  1. Size of dataset. Filter approaches can handle large datasets. Wrapper approaches require higher computational cost as the number of features increases since each new feature causes an increase in the number of subsets that need to be checked.
  2. Type of model. Tree-based models generate feature importance rankings as a part of model fitting, hence the use of embedded approach is more logical. In case of distance-based models like k-Nearest Neighbors, the use of filter or wrapper approaches is more appropriate.
  3. Need for interpretability. A feature may have an importance score in a trained model while never being considered by any feature selection technique. Wrapper and embedded approaches are more difficult to explain to an outsider as they rely on the performance of the model.

The Mistake That Breaks Everything: Selecting Before You Split

Performing feature selection on the whole dataset prior to the split of it into the training and test set results in the test set impacting the selected features, thus generating a biased accuracy figure that does not reflect the true accuracy figure for real unseen data.

The Mechanism: All the feature selection algorithms, among them the filter ones, compute certain metrics such as the correlation and the information gain based on all available rows. When the test set impacts the computation of those metrics, the selected features depend on the specific data on which the model will be scored.

The correct sequence is:

  1. Divide the dataset into training and testing datasets initially.
  2. Use the training dataset only to fit the feature selection algorithm.
  3. Use the selected features to both train and test datasets.
  4. Train the model using the training dataset with selected features.
  5. Test the model using the test dataset.

Nested cross-validation applies this same sequence within each cross-validation fold, so that feature selection is refit independently for every fold rather than once on the full dataset. RFECV and Pipeline in scikit-learn implement this automatically when feature selection is placed inside the pipeline object before cross-validation is run.

When Feature Selection Provides No Benefit

Feature Selection in ML

Feature selection does not improve every model. Three conditions determine when it adds no measurable value:

  1. Regularized linear models. LASSO and elastic nets select features as part of training. Performing any additional filter or wrapper step beforehand results in redundant computation by the algorithm itself.
  2. Gradient-boosted tree models. XGBoost, LightGBM, and CatBoost automatically give near zero importance scores to the unimportant features during training for data sets having a modest number of features. Any pre-processing step for such algorithms often results in no difference in accuracy whatsoever.
  3. Low feature counts relative to sample size. In the case where the number of features is few in comparison to the number of training samples, there is no curse of dimensionality and the overhead of feature selection exceeds its benefits.

The selection of features yields the greatest quantifiable gains in the case of datasets that are highly dimensional with very few sample points, when distance-based algorithms or linear models are not regularized. Feature selection is applicable to genomic data, as well as in the classification of text when using a bag-of-words model without further feature engineering.

Feature Selection in Python: A Working Example

The following example applies VarianceThreshold, SelectKBest, and RFECV inside a single scikit-learn Pipeline, so that feature selection is refit within each cross-validation fold rather than applied once to the full dataset.

from sklearn.pipeline import Pipeline

from sklearn.feature_selection import VarianceThreshold, SelectKBest, f_classif, RFECV

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import StratifiedKFold, cross_val_score

# Step 1: remove near-zero-variance features

variance_filter = VarianceThreshold(threshold=0.01)

# Step 2: filter method — keep the top 20 features by ANOVA F-value

filter_step = SelectKBest(score_func=f_classif, k=20)

# Step 3: wrapper method — recursive elimination with cross-validation

estimator = RandomForestClassifier(n_estimators=200, random_state=42)

wrapper_step = RFECV(estimator=estimator, step=1, cv=5, scoring="accuracy")

pipeline = Pipeline([

    ("variance_filter", variance_filter),

    ("filter", filter_step),

    ("wrapper", wrapper_step),

    ("model", estimator),

])

# Feature selection is refit inside each fold — not on the full dataset

cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

scores = cross_val_score(pipeline, X, y, cv=cv, scoring="accuracy")

print(f"Mean cross-validated accuracy: {scores.mean():.3f}")

Placing SelectKBest and RFECV inside the Pipeline object, and calling cross_val_score on the full pipeline, ensures scikit-learn refits both steps using only the training fold at each iteration. Calling SelectKBest or RFECV on the full dataset before cross_val_score reproduces the leakage error described in the previous section.

How Many Features Should You Keep?

The number of features to keep is determined by the point at which cross-validated accuracy stops improving, identified using RFECV‘s per-step validation scores rather than a fixed target count.

RFECV records the cross-validated score at each feature count as it eliminates features one at a time. Plotting these scores produces a curve that typically rises, plateaus, and then declines as informative features are removed alongside noise. The recommended feature count is the smallest number of features within one standard error of the peak score, rather than the exact peak, because the exact peak can reflect noise specific to the validation folds used.

Feature Importance Is Not Feature Selection

The importance of a feature refers to a score that indicates how important the particular feature is for predictions made by the trained model. Feature selection, on the other hand, is a process where the choice of the selected features for the model is made.

A feature may have an importance score in a certain trained model while being not at all considered by any of the feature selection techniques, and a feature that is filtered out by a filter technique never gets an importance score because it is not even included in the model training process.

MethodMeasuresComputed
SHAP valuesEach feature’s individual contribution to a specific predictionAfter training, using a game-theoretic allocation
Permutation importanceThe drop in model accuracy when a feature’s values are randomly shuffledAfter training, by re-scoring the model repeatedly
Gini importance (tree models)Average impurity reduction attributable to a feature across all treesDuring training, as a byproduct

Gini importance suffers from an inherent bias in favor of categorical variables with higher cardinality since the higher the cardinality of a feature, the more chances there will be for generating a split which results in a lower degree of impurity regardless of whether or not the feature has any relationship with the target.

Handling Correlated and Redundant Features

Highly correlated features carry similar information, and including two of them in a model increases the variance of the coefficient estimates without improving predictive power. 

The variance inflation factor (VIF) shows what increase in variance is caused by the correlation of the feature with all other features in the model.

VIF valueInterpretation
1No correlation with other features
1–5Moderate correlation, generally acceptable
5–10High correlation, review required
Above 10Severe multicollinearity, feature removal recommended

One such heuristic drops one feature of each pair whose correlation coefficient ranges between 0.85 and 0.90, retaining the one having a stronger association with the target variable.

Maximum Relevance Minimum Redundancy (mRMR) is an example of a filter approach where the selection of the features involves the simultaneous maximization of the relevance of the selected features to the target variable and minimization of redundancy with previously selected features, instead of individual ranking of the features. 

A correlated feature is not always a redundant one. Two features that are correlated with each other can both be relevant to the target variable, and dropping one without verifying its relevance to the target variable may lower the prediction accuracy of the model.

Common Feature Selection Mistakes

  1. Feature selection performed prior to splitting the data set. Creates an inflated accuracy estimate, as explained above.
  2. Relying on a single feature selection without assessing its stability.Running a feature selection wrapper or filter again on different subsets of data or cross-validation folds results in a different feature subset being selected each time. If the feature subset changes significantly in each iteration, then this implies the feature selection is noise-sensitive rather than stable.
  3. Assessing Gini importance of high cardinality categorical features without adjustment. Creates an inflated importance measure for high cardinality categorical features.
  4. Performing feature selection on time-series data without taking into account its temporal nature. Cross-validation randomly shuffles the data, which makes it possible for future-time information to leak during the feature selection procedure. Time-series feature selection needs a time-sorted validation split.
  5. Deleting a protected attribute without testing for the presence of proxy variables. Deletion of a feature such as ZIP code will not delete the information if there is a correlated variable such as the average income per neighborhood.

FAQs

Q1. What is feature selection in machine learning? 

Ans. It is the process of selecting a subset of input variables that are relevant from the whole data set that will be used for model training, while excluding any redundant or noisy variables.

Q2. What are the three types of feature selection? 

Ans. There are three approaches to feature selection: filter, that scores features based on some statistical tests; wrapper, that evaluates subsets of features based on the performance of models; and embedded, that selects features while training the model.

Q3. Is feature selection the same as dimensionality reduction? 

Ans. Not necessarily. There are several approaches to dimensionality reduction, among which are feature selection and feature extraction, where the latter creates transformed features based on existing ones.

Q4. Should feature selection happen before or after the train/test split? 

Ans. Feature selection should be done on the training set only, after the train/test split. If it is fitted on the whole dataset, information from the test set would be leaked into the feature selection process.

Q5. How many features should I select? 

Ans. Use RFECV to plot cross-validated accuracy against feature count, and select the smallest feature count within one standard error of the peak score.

Q6. Does XGBoost need feature selection? 

Ans. XGBoost and gradient boosted trees automatically allocate close-to-zero importance to irrelevant features in the training phase on datasets with medium size number of features, and therefore feature selection does not make any impact on the accuracy of the model.

Q7. Is PCA a feature selection method? 

Ans. Not really, PCA is a feature extraction method as it constructs the new features from the original features and does not select only a few of the original features.

Q8. Which feature selection method is fastest? 

Ans. Yes, since removing the feature that has predictive signals will decrease the accuracy of the model; this happens when a feature with correlation to other features is removed without checking for individual correlation with the target variable.

Q9. How do I perform feature selection in Python? 

Ans. Use scikit-learn’s VarianceThreshold, SelectKBest, and RFECV classes inside a Pipeline object, and run the pipeline through cross_val_score so that feature selection is refitted within each cross-validation fold.

Q10. Can feature selection hurt model performance? 

Ans. Yes, since removing the feature that has predictive signals will decrease the accuracy of the model; this happens when a feature with correlation to other features is removed without checking for individual correlation with the target variable.

Summary Comparison Table

Method typeModel-awareRelative costHandles feature interactionsCommon techniques
FilterNoLowNoChi-square, ANOVA, correlation, mutual information
WrapperYesHighYesForward selection, backward elimination, RFE, RFECV
EmbeddedYes (intrinsic)ModeratePartialLASSO, elastic net, random forest importance

Whether an accuracy figure presented is reliable depends on the order of operations. An approach to feature selection nested in a cross validation cycle and relying on the training set only in every repetition gives the accuracy that the classifier can get on new data. Otherwise, it won’t.

Gyansetu offers top professional training certification courses designed to enhance your skills and advance your career, providing industry-relevant knowledge and practical expertise.