Feature Engineering

GATE Relevance: Medium

🎯 Learning Objectives

  • Apply normalization and standardization to features
  • Encode categorical variables using one-hot and label encoding
  • Select features using filter, wrapper, and embedded methods
  • Handle missing data appropriately
  • Understand the curse of dimensionality

1. Feature Scaling

Many ML algorithms (KNN, SVM, gradient-based methods, PCA) are sensitive to the scale of input features. Features with large ranges dominate those with small ranges.

Min-Max Normalization

Scales features to a fixed range, typically [0, 1]:

x′ = (x − x_min) / (x_max − x_min)

  • Output range: [0, 1] (or any specified [a, b])
  • Sensitive to outliers (outliers compress the rest of the data)
  • Preserves the original distribution shape
Standardization (Z-Score Normalization)

Transforms features to have zero mean and unit variance:

z = (x − μ) / σ

  • Output: mean = 0, std = 1, range is unbounded
  • Less sensitive to outliers than min-max
  • Preferred for algorithms assuming Gaussian input (e.g., SVM with RBF kernel, PCA)
GATE Numerical: Feature Scaling

Q: Feature values: [10, 20, 30, 40, 50]. Apply (a) min-max normalization, (b) z-score standardization.

A (a): min=10, max=50. x′ = (x−10)/40. Result: [0, 0.25, 0.5, 0.75, 1.0]

A (b): μ=30, σ=√200 = 10√2 ≈ 14.14. z = (x−30)/14.14. Result: [−1.414, −0.707, 0, 0.707, 1.414]

PropertyMin-MaxStandardization
Output range[0, 1]Unbounded
Outlier sensitivityHighModerate
Preserves zerosNoOnly if μ=0
Best forNeural nets, image pixelsSVM, PCA, logistic regression
DistributionPreserves shapeCenters at 0
Fit on Training Data Only

Always compute scaling parameters (μ, σ, min, max) on the training set only, then apply the same transformation to the validation and test sets. Otherwise, you leak information from test data.

2. Categorical Encoding

ML algorithms work with numbers. Categorical features must be converted to numerical form.

Label Encoding

Assigns each category a unique integer: Red→0, Green→1, Blue→2.

  • Problem: Implies ordinal relationship (Green > Red). Only suitable for truly ordinal features like “Low, Medium, High”.
One-Hot Encoding

Creates a binary column for each category. K categories → K binary columns (or K−1 with dummy variable trap avoidance).

Color {Red, Green, Blue} becomes: Red=[1,0,0], Green=[0,1,0], Blue=[0,0,1]

  • No ordinal assumption
  • Increases dimensionality (problematic for high-cardinality features)
Dummy Variable Trap

When using one-hot encoding with linear models, drop one column (K−1 encoding) to avoid perfect multicollinearity. The dropped category becomes the “reference” category.

3. Feature Selection Methods

MethodApproachExamplesProsCons
FilterEvaluate features independently using statistical testsCorrelation, Chi-squared, Mutual Information, Variance ThresholdFast, model-agnosticIgnores feature interactions
WrapperUse a model to evaluate subsets of featuresForward Selection, Backward Elimination, Recursive Feature Elimination (RFE)Considers interactionsComputationally expensive
EmbeddedFeature selection built into the learning algorithmLasso (L1), Decision Tree feature importances, Ridge coefficientsEfficient, interaction-awareModel-specific

3.1 Filter Methods

Correlation-Based Selection

Remove features that have high correlation with each other (redundant) or low correlation with the target (irrelevant).

Pearson correlation: r = Cov(X,Y) / (σ_X × σ_Y). Range: [−1, 1].

3.2 Wrapper Methods

Forward Selection
1. Start with no features selected
2. For each remaining feature:
   a. Add it to the current set
   b. Evaluate model performance (e.g., with CV)
3. Keep the feature that gives the best improvement
4. Repeat until no improvement or desired number of features

4. Handling Missing Data

StrategyWhen to UseImpact
Drop rowsFew missing values, large datasetLoses data; can introduce bias if missing not at random
Drop columnsFeature has >50% missingLoses entire feature
Mean/Median imputationNumerical features, MCARSimple but reduces variance
Mode imputationCategorical featuresSimple; can distort distribution
KNN imputationModerate missing, features correlatedBetter quality; computationally expensive
Indicator variableMissingness itself is informativeAdds binary “is_missing” feature
Types of Missing Data
  • MCAR (Missing Completely At Random): Missingness unrelated to any variable
  • MAR (Missing At Random): Missingness related to observed data but not the missing value itself
  • MNAR (Missing Not At Random): Missingness related to the missing value (most problematic)

5. Curse of Dimensionality

The Curse of Dimensionality

As the number of features (dimensions) increases, the volume of the feature space grows exponentially, causing data to become increasingly sparse. This has profound effects on ML algorithms:

  • Distance metrics become meaningless: In high-D space, the ratio of nearest to farthest neighbour distances approaches 1
  • More data needed: To maintain the same density, sample size must grow exponentially with dimensions
  • Overfitting risk increases: More features → more parameters → easier to memorize training data
GATE Example: Dimensionality

Q: To keep 10 samples per “cell” with 10 bins per feature: 1D needs 10×10=100 samples, 2D needs 10×100=1000. How many for 5D?

A: 10 × 10⁵ = 10 × 100000 = 1,000,000 samples. Exponential growth!

Mitigation Strategies
  • Feature selection (reduce irrelevant features)
  • Dimensionality reduction (PCA, t-SNE)
  • Regularization (L1/L2)
  • Domain knowledge to select meaningful features

6. Feature Importance

After training a model, understanding which features contribute most to predictions is valuable for interpretation and feature selection.

Feature Importance Methods
  • Tree-based: Total reduction in impurity (Gini/Entropy) across all splits using that feature
  • Permutation importance: Shuffle one feature, measure drop in performance
  • Coefficient magnitude: |wᵢ| for linear models (after standardization)
  • L1 regularization: Features with zero coefficients are unimportant