Section 6: Feature Engineering

Learning Objectives

By the end of this section, students will be able to:

  • Transform raw data into effective features for machine learning
  • Apply scaling and normalization techniques
  • Handle categorical variables through encoding
  • Create meaningful interaction terms
  • Prepare data for dimensionality reduction

Introduction

Feature engineering transforms raw data into representations that algorithms can learn from effectively. This section teaches how to create meaningful features from real estate data for predictive modeling.

Main Content

The Art of Information Extraction

Feature engineering transforms raw data into representations that algorithms can learn from effectively. The process requires both technical skill and domain knowledge to identify which transformations will reveal hidden patterns. Raw data rarely arrives in a form optimal for machine learning. A date column contains information about seasonality, day of week effects, and holiday patterns, but algorithms cannot extract these insights from a simple timestamp.

The distinction between good and mediocre models often lies not in algorithm selection but in feature quality. A linear regression with well-engineered features frequently outperforms sophisticated neural networks trained on raw data. This reality shifts the focus from model complexity to data representation.

Consider the fundamental challenge: algorithms learn patterns through numerical operations. They compute distances, gradients, and probabilities. When features exist on vastly different scales or encode information inefficiently, these computations become distorted. A property’s age in years (ranging from 0 to 200) and its price in dollars (ranging from 50,000 to 5,000,000) create a space where price dominates all distance calculations, making age virtually invisible to distance-based algorithms.

Feature Engineering Tools Figure: Overview of key feature engineering tools and their primary purposes. Each tool addresses specific data transformation challenges in machine learning pipelines.

Variable Transformation and Scaling

The choice between transformation techniques depends on both data distribution and algorithm requirements. Standardization (z-score normalization) centers data around zero with unit variance, while normalization (min-max scaling) compresses values into a bounded range, typically [0,1]. These aren’t interchangeable techniques but tools suited for different scenarios.

Standardization transforms each feature using the formula below. This transformation preserves the shape of the original distribution while ensuring all features contribute equally to distance calculations. The resulting z-scores indicate how many standard deviations each value lies from the mean.

Z-Score Standardization Formula

z = (x - μ) / σ

Where:

  • z = standardized value (z-score)
  • x = original value
  • μ = population mean
  • σ = population standard deviation
  • Result: mean = 0, standard deviation = 1

When should you standardize versus normalize? The answer emerges from understanding your algorithm’s assumptions. Gradient-based methods like logistic regression and neural networks benefit from standardization because it centers the optimization landscape. Distance-based algorithms like k-nearest neighbors work well with either approach, but normalization provides interpretable bounded values.

Excel Implementation: Standardization

In Excel, you can implement standardization using built-in functions:

Step 1: Calculate the mean and standard deviation for each variable - Mean: =AVERAGE(B2:B6) for heart rate - Standard deviation: =STDEV(B2:B6) for heart rate

Step 2: Apply the standardization formula - Standardized value: =(B2-$B$8)/$B$9 - Where B8 contains the mean and B9 contains the standard deviation

Step 3: Copy the formula across all data points

This transforms each value into a z-score, showing how many standard deviations it lies from the mean.

Beyond linear scaling, logarithmic transformation addresses skewed distributions common in business data. Income, sales figures, and property prices often follow log-normal distributions where a few extreme values distort the mean. The logarithm compresses the scale of large values while expanding the scale of small values, creating more symmetric distributions.

A marketing analyst examining customer lifetime value finds that 5% of customers generate 60% of revenue. The raw distribution shows a long right tail with values ranging from $10 to $50,000. Applying log transformation creates a more normal distribution where statistical assumptions hold and outliers don’t dominate model training. The transformed feature log(CLV + 1) ranges from 2.4 to 10.8, allowing the model to learn patterns across all customer segments rather than optimizing only for high-value outliers.

Categorical Encoding Strategies

Categorical variables present a unique challenge because mathematical operations on arbitrary category labels lack meaning. The transformation from categories to numbers must preserve the information while enabling computation. Different encoding strategies suit different types of categorical relationships.

One-hot encoding creates binary columns for each category, setting exactly one to 1 and others to 0. This approach works well for nominal variables without inherent order. However, it suffers from the curse of dimensionality when dealing with high-cardinality features. A city variable with 1,000 unique values creates 1,000 new columns, most containing zeros.

Ordinal encoding assigns integer values to categories with natural ordering. Education levels (high school, bachelor’s, master’s, doctorate) map to (1, 2, 3, 4). The danger lies in assuming equal intervals between categories. The difference between high school and bachelor’s might not equal the difference between master’s and doctorate in terms of predictive power.

Target encoding replaces categories with their target variable statistics, typically the mean. This approach reduces dimensionality while incorporating target information directly into the feature. The risk of data leakage requires careful implementation using only training data statistics or cross-validation schemes to compute encodings.

Excel Implementation: Categorical Encoding

One-Hot Encoding in Excel: 1. Create separate columns for each category 2. Use IF statements: =IF(B2="condo",1,0) for condo column 3. Repeat for each category type

Target Encoding in Excel: 1. Calculate mean price by neighborhood: =AVERAGEIF(C:C,"downtown",D:D) 2. Create lookup table with neighborhood means 3. Use VLOOKUP to replace categories: =VLOOKUP(C2,lookup_table,2,FALSE)

This approach prevents the dimensionality explosion while incorporating target information.

The frequency of rare categories influences encoding decisions. Categories appearing in less than 1% of records often lack statistical significance. Grouping them into an “other” category or using frequency encoding (replacing categories with their occurrence counts) preserves some information while reducing noise.

Interaction Term Creation

Individual features tell partial stories. The relationship between features often contains more predictive power than features in isolation. Interaction terms capture these relationships mathematically, allowing linear models to learn non-linear patterns.

The simplest interactions multiply two features: area × price_per_sqft reveals total property value. But meaningful interactions require field knowledge. In healthcare, the interaction between age and medication dosage affects treatment efficacy differently than either factor alone. In marketing, the interaction between ad spend and seasonality determines campaign effectiveness.

Polynomial features extend interactions to higher orders and self-interactions. The square of a feature captures non-linear relationships within that single variable. Income squared might better predict loan default risk than income alone, as both very low and very high incomes correlate with different risk profiles.

Should you create all possible interactions? The combinatorial explosion quickly becomes unmanageable. With just 20 features, all pairwise interactions create 190 additional features. Including squared terms adds another 20. Three-way interactions would add 1,140 more. Feature selection becomes critical when engineering interactions.

Excel Implementation: Interaction Terms

Creating Meaningful Interactions: 1. Debt-to-Income Ratio: =C2/B2 (debt divided by income) 2. Payment Capacity: =B2*(D2/850) (income weighted by credit score) 3. Risk-Adjusted Amount: =E2/(D2/100) (loan amount adjusted for credit risk) 4. Income Squared: =B2^2 (captures non-linear income effects) 5. Credit-Income Interaction: =B2*D2 (multiplies income and credit score)

These formulas create domain-specific features that capture business relationships rather than arbitrary mathematical combinations.

Statistical methods help identify promising interactions. Random forests naturally discover interactions through their tree structure. Examining feature importances from tree-based models reveals which combinations drive predictions. These discovered interactions can then be explicitly engineered for use in simpler models.

Dimensionality Reduction Preparation

High-dimensional data creates computational and statistical challenges. More features than observations guarantee overfitting. Even with sufficient observations, irrelevant features add noise that obscures true patterns. Feature selection and dimensionality reduction address these challenges through different approaches.

Feature selection identifies the most informative subset of existing features. Filter methods evaluate features independently using statistical tests (chi-squared for categorical targets, ANOVA for continuous). Wrapper methods evaluate feature subsets by training models and measuring performance. Embedded methods like LASSO regression perform selection during model training through regularization. Note: Advanced methods like LASSO, PCA, and other dimensionality reduction techniques will be covered in detail in Module 2.

The correlation matrix reveals redundant features. When two features correlate above 0.95, they likely convey the same information. Removing one reduces dimensionality without information loss. But correlation only captures linear relationships. Two features with low correlation might still be redundant if they have non-linear dependencies.

A financial institution analyzing loan applications has 200 features including credit history, employment data, and financial ratios. Many ratios derive from the same underlying values: debt-to-income, debt-to-assets, and income-to-assets are mathematically related. Principal Component Analysis reveals that 15 components explain 95% of variance. Rather than feeding all 200 features to the model, the bank uses these 15 components, reducing training time and improving generalization while preserving most information.

Variance thresholds provide a simple filter. Features with near-zero variance contribute no information. A binary feature that equals 1 for only three observations in a million-record dataset cannot meaningfully influence predictions. Removing such features reduces noise and computational burden.

Binning continuous variables into discrete ranges serves as both dimensionality reduction and non-linearity capture. Age becomes age groups, income becomes income brackets. This transformation loses granularity but can reveal threshold effects. The relationship between age and insurance claims might be flat within age ranges but jump significantly at certain thresholds.

Visual Example: Age Binning

Consider a dataset with individual ages from 18 to 85. Instead of using exact age, we create meaningful groups:

CONTINUOUS AGE DATA          BINNED AGE GROUPS
┌─────────────────────┐     ┌─────────────────────┐
│ 23, 24, 25, 26...   │────▶│ Young Adult (18-30) │
│ 31, 32, 33, 34...   │     │ Adult (31-50)       │
│ 51, 52, 53, 54...   │     │ Middle-aged (51-65) │
│ 66, 67, 68, 69...   │     │ Senior (66+)        │
└─────────────────────┘     └─────────────────────┘

Benefits:

  • Reduces noise: Small age differences within groups become irrelevant
  • Captures thresholds: Insurance rates might jump at age 65 (Medicare eligibility)
  • Simplifies models: 4 categories instead of 68 possible ages
  • Reveals patterns: Groups might have very different risk profiles

Trade-offs:

  • Loses precision: 25-year-old and 30-year-old treated identically
  • Arbitrary boundaries: Why 30 and not 29 or 31?
  • Information loss: Exact age relationships disappear

Excel Implementation: Binning

Creating Age Groups: 1. Use nested IF statements: =IF(B2<=30,"Young",IF(B2<=50,"Middle",IF(B2<=65,"Senior","Elderly"))) 2. Or use VLOOKUP with a lookup table for cleaner formulas

Creating BMI Categories: 1. Medical thresholds: =IF(C2<18.5,"Underweight",IF(C2<25,"Normal",IF(C2<30,"Overweight","Obese")))

Converting to Numbers: 1. Create a mapping table (Young=1, Middle=2, Senior=3, Elderly=4) 2. Use VLOOKUP: =VLOOKUP(D2,mapping_table,2,FALSE)

This approach reduces noise while preserving meaningful categorical information for modeling.

The Feature Engineering Pipeline

Feature engineering isn’t a one-time transformation but an iterative process integrated into the machine learning pipeline. Each transformation must be reproducible on new data. Training data determines transformation parameters (means for standardization, mappings for encoding), which then apply consistently to validation and test sets.

When does feature engineering cross into overfitting? The line blurs when transformations become too specific to training data. Creating a feature that perfectly separates classes in training won’t generalize. Cross-validation helps identify over-engineered features whose performance drops significantly on held-out data.

The sequence of transformations matters. Scaling before creating polynomial features produces different results than the reverse order. Encoding categories before splitting data risks leakage. A well-designed pipeline ensures correct ordering and prevents information from flowing backward from test to training data.

Automated feature engineering tools like Featuretools accelerate the discovery process but require careful validation. They excel at systematic transformation but lack field knowledge. The most effective approach combines automated generation with human expertise to identify which engineered features align with business logic and theoretical foundations.

Validation and Impact Assessment

The ultimate test of feature engineering is model performance, but intermediate validations catch issues early. Visualizing engineered features reveals whether transformations achieved their intended effects. A histogram of log-transformed values should appear more normal than the original. Scaled features should have comparable ranges.

Feature importance analysis post-training indicates which engineered features contribute to predictions. Low-importance engineered features might indicate unsuccessful transformations or redundancy with existing features. High-importance engineered features validate the engineering effort and might suggest related transformations worth exploring.

The relationship between feature engineering complexity and model performance follows diminishing returns. Initial transformations often yield substantial improvements. Each additional round of engineering provides smaller gains while increasing pipeline complexity and maintenance burden. The optimal stopping point balances performance gains against implementation costs.

Business constraints shape feature engineering decisions. Real-time prediction systems cannot afford complex transformations that add latency. Regulated industries require interpretable features that stakeholders can understand and audit. The best engineered features improve both model performance and human understanding of the problem area.

Example: Real Estate Feature Engineering

Building features for a property valuation model.

Raw Data: - Property address, square footage, bedrooms, bathrooms - Year built, lot size, neighborhood - Recent sale price

Engineered Features:

  • Age = Current year - Year built
  • Price per square foot = Sale price / Square footage
  • Bedroom-to-bathroom ratio = Bedrooms / Bathrooms
  • Lot size per square foot = Lot size / Square footage
  • Neighborhood price index = Average neighborhood price / City average

© 2025 Prof. Tim Frenzel. All rights reserved. | Version 1.0.5