Feature Selection
Complete learning notes
1. Introduction
Not every column in a dataset is actually useful for making predictions — some are irrelevant, some are redundant (duplicating information already captured by other features), and including them can actually hurt model performance. Feature Selection is the process of choosing only the most relevant features to use in your model.
2. What is Feature Selection?
Simple definition: Feature Selection is the process of choosing a subset of the most relevant, useful features from a dataset, removing those that are irrelevant or redundant.
Technical explanation: Feature Selection techniques identify and retain the features most predictive of the target variable while discarding irrelevant or highly redundant ones, reducing dimensionality, improving model interpretability, and often improving generalization performance.
3. Why is it Important?
- Irrelevant or redundant features can add noise, making it harder for a model to learn genuinely useful patterns.
- Fewer, well-chosen features often lead to simpler, faster-training, more interpretable models.
- High-dimensional data (many features) can suffer from the "curse of dimensionality," where models struggle to learn effectively as the number of features grows.
4. Prerequisites
Comfort with Basic Statistics (correlation, Module 1 Topic 8) and Features & Labels (Module 2, Topic 9).
5. Core Concepts
- Why irrelevant/redundant features hurt models
- Correlation-based feature selection
- Variance Threshold (removing near-constant features)
- Selecting features most correlated with the target
6. Detailed Explanation
a) Why Irrelevant/Redundant Features Hurt Models
Irrelevant features add noise without adding predictive value, while highly correlated (redundant) features essentially provide duplicate information, potentially confusing certain algorithms and increasing computational cost without any real benefit.
b) Correlation-Based Feature Selection
By examining the correlation matrix of a dataset, you can identify features that are highly correlated with the target (potentially valuable) and features that are highly correlated with each other (potentially redundant — you may only need to keep one of them).
c) Variance Threshold
A feature with very low variance (i.e., almost the same value across all rows) provides little useful information for distinguishing between different outcomes, and can often be safely removed.
d) Selecting Features Correlated with the Target
Features with a strong correlation (positive or negative) with the target variable are generally strong candidates to keep; features with correlation near zero may be safe to remove, though this should be combined with domain knowledge rather than applied blindly.
7. How It Works
- Compute the correlation matrix for your dataset's numeric features.
- Identify features highly correlated with each other (redundant) — consider removing one from each such pair.
- Identify features weakly correlated with the target — consider whether they're genuinely useful or safe to remove.
- Optionally, apply a variance threshold to remove near-constant features.
- Validate your feature selection decisions by checking model performance with and without the selected features.
8. Real-World Example
In a house price prediction dataset, "square footage" and "number of rooms" might be highly correlated with each other (larger houses tend to have more rooms) — keeping only one of these might simplify the model without losing much predictive power. Meanwhile, a column like "house's exterior paint color" is likely to have very low correlation with price and could reasonably be excluded.
9. Python Example
pythonimport pandas as pd data = { "square_footage": [1000, 1500, 1200, 1800, 2000], "num_rooms": [2, 3, 2, 4, 4], "paint_color_code": [1, 1, 2, 1, 2], # arbitrary, likely irrelevant "price": [200000, 280000, 230000, 320000, 350000] } df = pd.DataFrame(data) # Correlation matrix correlation_matrix = df.corr() print("Correlation matrix:") print(correlation_matrix) # Correlation specifically with the target ("price") print("\nCorrelation with price:") print(correlation_matrix["price"].sort_values(ascending=False))
Expected Output (approximate):
textCorrelation matrix: square_footage num_rooms paint_color_code price square_footage 1.000 0.985 0.086 0.997 num_rooms 0.985 1.000 -0.056 0.980 paint_color_code 0.086 -0.056 1.000 0.045 price 0.997 0.980 0.045 1.000 Correlation with price: price 1.000000 square_footage 0.997000 num_rooms 0.980000 paint_color_code 0.045000 Name: price, dtype: float64
10. Code Explanation
df.corr()calculates the pairwise correlation between every numeric column in the dataset, producing a correlation matrix.- Values close to +1 or -1 indicate strong relationships; values close to 0 indicate weak or no linear relationship.
- Sorting the "price" column of the correlation matrix reveals that
square_footageandnum_roomsare strongly correlated with price (good candidates to keep), whilepaint_color_codeshows almost no correlation (a strong candidate for removal). - Notice also that
square_footageandnum_roomsare highly correlated with EACH OTHER (0.985) — suggesting some redundancy between them, which could be addressed by keeping just one, depending on the specific modeling goal.
11. Advantages
- Simplifies models, often improving interpretability and training speed.
- Can improve model generalization by removing noisy, irrelevant features.
- Correlation-based analysis is quick, intuitive, and doesn't require training a full model first.
12. Limitations
- Correlation only captures linear relationships — a feature might be genuinely useful through a non-linear relationship that correlation alone won't detect.
- Removing features based purely on statistics, without domain knowledge, risks discarding genuinely important information.
- Feature selection decisions made on a specific dataset may not always generalize perfectly to new data.
13. Common Mistakes
- Removing features solely based on low correlation with the target, without considering possible non-linear relationships or interactions with other features.
- Ignoring the difference between "feature selection" (choosing which existing features to keep) and "feature engineering" (creating new, potentially more useful features) — the two are related but distinct.
- Not validating that removing a feature actually helps (or at least doesn't hurt) model performance in practice.
14. Best Practices
- Use correlation analysis as a starting point, not a final decision — combine it with domain knowledge.
- Test model performance both with and without a candidate feature before finalizing its removal.
- Be cautious about removing features solely due to low linear correlation, since some relationships are non-linear.
15. Real-World Applications
- Simplifying large, high-dimensional datasets like genomic data or extensive customer records before modeling.
- Improving interpretability of models used for business or medical decision-making, where fewer, clearer features are preferred.
- Reducing training time and computational cost for large-scale ML systems.
16. Interview-Oriented Points
- Be ready to explain why irrelevant or redundant features can hurt model performance.
- Understand how a correlation matrix can help identify both useful features and redundant ones.
- Be able to explain the difference between Feature Selection and Feature Engineering (explored further in Module 7).
17. Exam-Oriented Points
- Feature Selection removes irrelevant or redundant features, keeping only the most useful ones.
df.corr()computes a correlation matrix, helping identify relationships between features and the target.- Features with very low variance or very low correlation with the target are common candidates for removal.
18. Comparison Table — Feature Selection vs Feature Engineering
| Aspect | Feature Selection | Feature Engineering |
|---|---|---|
| Goal | Choose the best subset of EXISTING features | Create NEW, potentially more useful features |
| Example action | Removing a low-correlation column | Combining "height" and "weight" into a new "BMI" feature |
| Effect on feature count | Reduces the number of features | Can increase the number of features |
| When covered in this course | Module 3 (this topic) | Module 7 — Improving ML Models |
19. Quick Revision
- Feature Selection removes irrelevant or redundant features to simplify and improve a model.
- A correlation matrix (
df.corr()) helps identify features strongly related to the target, and features that are redundant with each other. - Low-variance (near-constant) features are also common candidates for removal.
- Always validate feature selection decisions by checking actual model performance, not statistics alone.