CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
  • Week 1
    • 1.The ML Lifecycle (from a Specialty Perspective)
    • 2.Data Storage for ML: S3, EFS, FSx for Lustre, and Data Formats
    • 3.Data Ingestion: Kinesis, Glue, Batch vs. Streaming
    • 4.Data Labeling: SageMaker Ground Truth, Active Learning, Label Quality
    • 5.Week 1 Comprehensive Review: ML Overview & Data Engineering 1
  • Week 2
  • Week 3
  • Week 4
    • 1.Dimensionality Reduction: PCA, t-SNE, and the Curse of Dimensionality
    • 2.Feature Selection: Filter, Wrapper and Embedded Methods, Importance, and Multicollinearity
    • 3.Data Visualization: Distributions, Correlations, QuickSight, and Drawing Insights
    • 4.Handling Class Imbalance: Oversampling, Undersampling, SMOTE, Class Weights, Evaluation
    • 5.Week 4 Comprehensive Review: Dimensionality, Feature Selection, Visualization, Imbalance
  • Week 5
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
  • Week 12
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← MLS-C01/Week 4/Day 2

Based on the MLS-C01 exam guide·Report an error

MLS-C01· AssociateWeek 4 · Day 2~30 min read

Day 2 - Feature Selection: Filter, Wrapper and Embedded Methods, Importance, and Multicollinearity

📌 Key Takeaways

  • Feature selection keeps a subset of the original features, so interpretability is preserved. PCA creates new axes and loses meaning.
  • Three approaches: filter (fast, model-agnostic) / wrapper (accurate, very slow) / embedded (balanced; Lasso and tree importance).
  • L1 (Lasso) drives coefficients to exactly 0, giving automatic selection. L2 (Ridge) only shrinks them.
  • For importance, impurity-based (MDI) importance is biased toward high-cardinality features. Permutation importance and SHAP are more trustworthy.
  • Multicollinearity is diagnosed with the correlation matrix (|r| > 0.8–0.9) and VIF (> 5 caution, > 10 severe). It is fatal to linear models; trees are insensitive.

Why Feature Selection?

If Day 1's dimensionality reduction was "creating new synthetic axes," feature selection is "keeping only the useful existing features." Both reduce dimensions, but selection preserves the interpretability of the original features.

  • Less overfitting: irrelevant features let the model learn noise
  • Faster training and inference
  • Higher interpretability: the fewer the features, the easier the model is to explain
  • Mitigates the curse of dimensionality

Feature selection and PCA both reduce dimensions, but their philosophies differ. PCA linearly combines all the original features to create new axes, so the physical meaning of the result is lost. Selection keeps a subset of the original features as they are, so it is preferred in regulated industries (finance, healthcare) where you must explain "which variables were used for prediction."

Three Approaches

ApproachHow it worksStrengthWeaknessExamples
FilterScores features with statistical measures, independent of any modelFast, model-independentIgnores interactions between featuresCorrelation coefficient, chi-square, variance threshold, mutual information
WrapperRepeatedly trains and evaluates a model on feature subsetsCaptures interactions, accurateVery slow, overfitting riskForward selection, backward elimination, RFE
EmbeddedSelection is built into the model's training processBalance of efficiency and accuracyTied to the modelLasso (L1), tree feature importance

Which Approach Should You Pick?

SituationApproach to pickReason
Thousands of features, goal is a first-pass screenFilterScoring without model training → fastest
Dozens of features, goal is the best performanceWrapper (RFE)A scale at which the cost of repeated training is affordable
You want to cut features automatically in a linear modelEmbedded (Lasso)Training and selection in one step
You are already using trees or boostingEmbedded (tree importance)Importance comes as a by-product of training
The relationship is nonlinear so the correlation is near 0Filter (mutual information)Measures dependence not limited to linear

Filter Methods

Each feature is evaluated independently by its relationship to the target. There is no model training, so this is the fastest approach.

from sklearn.feature_selection import SelectKBest, f_classif, mutual_info_classif
 
# Select the top k features with the ANOVA F-test
selector = SelectKBest(score_func=f_classif, k=20)
X_filtered = selector.fit_transform(X_train, y_train)
 
# Use mutual information for nonlinear relationships
mi_selector = SelectKBest(score_func=mutual_info_classif, k=20)

Which statistic to use in a filter is determined by the combination of feature type and target type.

Feature typeTarget typeMetric usedNotes
NumericClassificationANOVA F-test (f_classif)Looks at mean differences between groups
CategoricalClassificationChi-square (chi2)Input must be non-negative
NumericRegressionPearson correlation / F-regression (f_regression)Assumes a linear relationship
AnyAnyMutual information (mutual_info_*)Also captures nonlinear dependence
AnyNone (unsupervised)Variance threshold (VarianceThreshold)For removing constant and quasi-constant columns

Wrapper Method: RFE

RFE (Recursive Feature Elimination) repeats the process of training a model and removing the weakest feature.

from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier
 
rfe = RFE(estimator=RandomForestClassifier(), n_features_to_select=15)
rfe.fit(X_train, y_train)
selected = X_train.columns[rfe.support_]

Embedded Method: L1 Regularization

from sklearn.linear_model import Lasso
 
lasso = Lasso(alpha=0.01)
lasso.fit(X_scaled, y_train)
# Features with a coefficient of 0 are removed automatically
selected = X_train.columns[lasso.coef_ != 0]
      L1 (Lasso) constraint     L2 (Ridge) constraint
          ╱╲                        ╭───╮
        ╱    ╲                     │     │
        ╲    ╱                     │     │
          ╲╱                        ╰───╯
   Loss contours easily meet     The contact point rarely lands on an axis
   at a vertex → coefficient = 0 → it only gets close to 0, never exactly 0
        (sparse solution)            (shrinkage only)

Lasso performs feature selection because of the geometry of the L1 penalty. The L1 constraint region is diamond-shaped, so the loss contours are likely to meet at a vertex (on an axis, i.e., coefficient = 0). L2 (Ridge) is circular, so although it shrinks coefficients toward 0, it rarely sets them exactly to 0. That is why "if you need a sparse solution, use L1."

⚠️ Pitfall: Lasso penalizes coefficient magnitude, so scaling must come first. Without standardization, the coefficient of a large-unit variable looks small and gets pushed to 0 first.

⚠️ Pitfall: Feature selection is also a data-dependent transformation. If you compute correlations or importance on all the data, select features, and then cross-validate, validation-set information leaks into the selection. Selection must be put in the pipeline and performed only on the training portion of each fold.

Feature Importance

Tree-based models provide feature importance after training.

MethodDescriptionCaution
Impurity-based (Gini/MDI)Sum of the impurity decrease from splitsBiased toward high-cardinality features
Permutation importanceShuffles a feature and measures the performance dropModel-agnostic, but double-counts correlated features
SHAP valuesContribution decomposition based on game theoryExpensive to compute, but the most consistent
import shap
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)
shap.summary_plot(shap_values, X_test)   # Visualize feature contributions
PurposeMethod to pickReason
A quick rough picture right after trainingImpurity-based (MDI)Immediate, no extra computation
Compare global importance without biasPermutation importanceMeasured as the actual performance drop on the trained model
Explain an individual prediction (why is this case fraud?)SHAPProvides a local explanation for each prediction
Model types differ and you must comparePermutation importance / SHAPCan be computed model-agnostically

Impurity-based importance is biased toward overestimating features with many split candidates (continuous, many-category). Permutation importance randomly shuffles a feature in a trained model and measures the performance drop, so it avoids this bias. SHAP additively decomposes feature contributions for each prediction, providing both global and local explanations, but is expensive to compute. For the exam, remember: "impurity-based is biased toward high cardinality; permutation and SHAP are more trustworthy."

Multicollinearity

When two or more features are strongly correlated, multicollinearity arises. In linear models, coefficients become unstable and interpretation is distorted.

Diagnostic metrics:

MetricThreshold
Correlation matrixSuspect when |r| > 0.8–0.9
VIF (Variance Inflation Factor)VIF > 5 (caution), > 10 (severe)
from statsmodels.stats.outliers_influence import variance_inflation_factor
import pandas as pd
 
vif = pd.DataFrame()
vif["feature"] = X.columns
vif["VIF"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]

Remedies: remove one of the correlated features, merge them with PCA, or mitigate with L2 regularization (Ridge).

Multicollinearity Diagnosis → Remedy

SymptomDiagnosisRemedy
Two features have |r| of 0.95Correlation matrixRemove one of the two (the one less important by domain)
Many features have VIF above 10VIFMerge them into orthogonal axes with PCA
Coefficient signs flip against common senseUnstable coefficientsMitigate with Ridge (L2), which suppresses coefficient variance
Coefficients swing widely when the sample changes slightlyResampling stabilityReduce features and retrain
Predictive performance is good but it cannot be interpreted—If prediction is the only goal, switching to a tree family is also an option

Multicollinearity Sensitivity by Model

ModelSensitivityResulting effect
Linear/logistic regressionVery highCoefficient variance explodes, signs flip, uninterpretable
Ridge / LassoMediumL2 stabilizes coefficients; L1 arbitrarily keeps only one of a correlated group
SVM (linear kernel)MediumCoefficient interpretation wobbles; predictions hold up relatively well
Decision Tree / RF / XGBoostLowSplits look at one feature at a time, so prediction is not much affected
KNN / K-meansMediumCorrelated features effectively count the same axis twice, distorting distance weighting

VIF is computed as VIF = 1/(1−R²), where R² is the coefficient of determination from regressing one feature on all the other features. As R² approaches 1 (that is, as the feature becomes almost perfectly predictable from the others), VIF explodes. Note: tree-based models are insensitive to multicollinearity — because each split uses only one feature at a time. Multicollinearity is fatal in linear models, where coefficient interpretation matters.

When two correlated features exist in a tree model, the importance is split between them and each shows up low. If you judge "the importance is low, so it can be discarded," you may in fact throw away an important signal wholesale.

Practical Selection Order

Feature selection is not a one-shot job but a funnel that filters in order from the cheapest step.

[Hundreds to thousands of raw features]
        │
  ① Remove constant · quasi-constant columns   (variance threshold. Axes with no information)
        │
  ② Remove ID · suspected-leakage columns      (columns that make the model memorize rows)
        │
  ③ Filter: score by correlation · F-test · mutual information → top k
        │
  ④ Clean up multicollinearity                 (|r| > 0.8–0.9, VIF > 5–10)
        │
  ⑤ Embedded: further reduction with Lasso or tree importance
        │
  ⑥ (If you can afford it) Wrapper: final fine-tuning with RFE
        │
  ⑦ Confirm performance with cross-validation — do ③–⑥ only on the training portion of each fold
StepCostRemaining feature count (example)
① Constant removalNearly 0Thousands → thousands
③ FilterLowThousands → tens to hundreds
⑤ EmbeddedMedium (one model training)Hundreds → tens
⑥ Wrapper (RFE)High (repeated model training)Tens → final

AWS Service Mapping

SituationService / featureReason
You want to view distributions and correlations before training and trim featuresSageMaker Data WranglerProvides visual data preparation and distribution/correlation reports
You must explain feature contributions to model predictionsSageMaker ClarifyProduces SHAP-based feature attribution
Experiment directly with filters, wrappers and Lasso in a notebookSageMaker notebook + scikit-learnFree combination of SelectKBest, RFE, Lasso, etc.
Features are correlated and you want to merge them into orthogonal axesSageMaker built-in PCAGives up interpretation in exchange for resolving multicollinearity

Question Clue → Answer Mapping

Clue in the questionAnswer to chooseReason
"Thousands of features, the fastest first-pass screen"Filter (mutual information / F-test)No model training needed
"Drive coefficients to exactly 0 for automatic selection"L1 (Lasso)Diamond constraint → sparse solution
"Shrink coefficients but never make them 0"L2 (Ridge)Circular constraint → shrinkage only
"Two features correlated at 0.95, coefficients unstable"Check VIF, then remove oneA classic symptom of multicollinearity
"The importance of a high-cardinality feature is oddly high"Impurity-based bias → recheck with permutation importanceA known bias of MDI
"You must explain which variables were used for prediction"Feature selection (not PCA)Keeps the original features
"The correlation coefficient is 0 but a relationship seems to exist"Mutual information / SpearmanPearson captures only linear relationships
"It is a tree model; is multicollinearity handling needed?"Generally not neededSplits use only one feature at a time

The next post covers data visualization, for checking distributions and correlations by eye, and AWS's BI service, QuickSight.

📖 Glossary

  • Feature selection: Dimensionality reduction that picks and keeps only the useful features among the original ones. Column names are preserved.
  • Filter method: A way of scoring and picking features using only statistics such as correlation coefficients or chi-square, without training a model.
  • Wrapper method: A way of finding the best combination by training and evaluating an actual model while changing feature combinations. Accurate but slow.
  • Embedded method: A way in which selection happens together with the model's training (Lasso, tree importance).
  • RFE (Recursive Feature Elimination): A wrapper technique that repeats training a model, dropping the weakest feature, and training again.
  • Mutual information: A measure of how much information two variables share. Also captures nonlinear relationships.
  • Sparse solution: A solution in which many coefficients are exactly 0. Produced by L1 regularization.
  • Impurity-based importance (MDI): Importance obtained by summing the impurity reduction when a tree splits. Overestimates features with many split candidates.
  • Permutation importance: Importance measured by how much performance drops after the feature's values are randomly shuffled.
  • Multicollinearity / VIF: A state in which features are strongly correlated with each other / a metric measuring that degree. Above 5 is caution, above 10 is severe.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

In data with thousands of features, you want to quickly do a first-pass filter of irrelevant features before training a model. Which approach is MOST appropriate?

Question 2

In a linear regression model, you want to obtain an automatic feature selection effect by driving the coefficients of some features to exactly 0. Which regularization is appropriate?

Question 3

The correlation coefficient of two features is 0.95 and the linear regression coefficients are unstable. What is the most appropriate diagnosis and treatment?

Question 4

Which statement about impurity-based feature importance in tree-based models is correct?

Question 5

In a regulated industry, you must explain "which variables were used in the prediction." Which is the most appropriate reason for preferring feature selection over PCA?

PreviousDimensionality Reduction: PCA, t-SNE, and the Curse of DimensionalityWeek 4 · Day 1Next Data Visualization: Distributions, Correlations, QuickSight, and Drawing InsightsWeek 4 · Day 3

Official references

  • MLS-C01 official exam page(opens in a new tab)
  • Amazon SageMaker AI documentation(opens in a new tab)

On this page

  • Key Takeaways
  • Why Feature Selection?
  • Three Approaches
  • Which Approach Should You Pick?
  • Filter Methods
  • Wrapper Method: RFE
  • Embedded Method: L1 Regularization
  • Feature Importance
  • Multicollinearity
  • Multicollinearity Diagnosis → Remedy
  • Multicollinearity Sensitivity by Model
  • Practical Selection Order
  • AWS Service Mapping
  • Question Clue → Answer Mapping
  • Glossary
  • Practice Questions