Feature Engineering

What Is Feature Engineering?

Feature engineering is the process of creating, selecting, and transforming input variables (features) to help a machine learning model learn more effectively. Raw data is often not in the best form for a model to use directly.

House Price Prediction

Raw features — House size, Bedrooms, Year built

Possible engineered features

  • Age of house (current year − year built)
  • Price per square foot
  • Bedroom density (bedrooms / house size)
  • Distance to city center

Good Features vs Poor Features

Good Features
  • Directly related to the prediction target
  • Consistent and rarely missing
  • Distinct from other features
  • Available at prediction time
Poor Features
  • Weakly related to the target
  • Frequently missing or inconsistent
  • Duplicates other features
  • Not available at prediction time
Exam Score Prediction

Useful — Study hours, Previous exam scores, Attendance rate

Less useful — Student ID, Favorite color, Random noise

What Makes a Good Feature

Good Feature Qualities
  • Relevant — Related to the prediction target
  • Available — Present at the time of prediction
  • Reliable — Consistent and not missing for most examples
  • Distinct — Not duplicating another feature
  • Interpretable — Understandable by humans

Creating New Features

Year of Birth → Age

Raw feature — Year of birth: 1998

Engineered feature — Age: 2026 − 1998 = 28

Age is often more useful than birth year because the relationship between age and outcomes is usually more direct.

Feature Transformation Examples
  • Logarithm of a skewed variable
  • Square root for count data
  • Polynomial features (x², x × y)
  • Ratio features (price per square foot)
  • Interaction terms (study hours × attendance)

Numerical Features

Numerical Features and Transformations

Original — House Size, Income, Age

  • Normalization (scale to 0–1 range)
  • Standardization (center at mean 0)
  • Logarithm (for skewed data)
  • Binning (group into ranges like Low/Medium/High)

Categorical Features

Categorical Features and Approaches

Original — Color, Country, Product Category

  • Label Encoding — Assign integer to each category
  • One-Hot Encoding — Binary vector for each category
  • Target Encoding — Replace with mean of label
  • Frequency Encoding — Replace with how often it appears

Text Features

Text Feature Engineering

Raw text — "This product is amazing!"

Engineered features

  • Word count: 5
  • Sentiment score: +0.9 (positive)
  • TF-IDF vector representation
  • Word embeddings (dense numerical vectors)

Image Features

Image Feature Examples
  • Pixel intensity histograms
  • Edge detection outputs
  • Color distributions
  • Object counts from detection models

Medical imaging — May use texture analysis or edge detection to find patterns not visible from raw pixels alone

Date and Time Features

Date: 2026-06-09 15:30
  • Year: 2026
  • Month: 6 (June)
  • Day of week: Tuesday
  • Hour: 15
  • Is weekend: No
  • Season: Summer

Time features help models learn patterns related to cycles and seasonality.

Location Features

Address → Location Features

Raw — "123 Main Street, Toronto"

  • Latitude and longitude
  • Distance to city center
  • Distance to nearest school
  • Neighborhood name
  • Region or state

Interaction Features

Example

Study Hours × Attendance Rate → Combined Learning Effort

This can be more informative than either feature alone because both variables together describe effort better than either individually.

Ratio Features

Ratio Examples

Price per square foot = price / house size

Clicks per visit = clicks / total visits

Revenue per customer = revenue / customers

House Price Ratio Example

House size: 1500 sq ft | Price: $300,000

Ratio: $300,000 / 1500 = $200 per sq ft

Binning

Binning groups continuous values into discrete categories. This can help when the relationship between a feature and the target is not linear.

Age and Income Bins

Age — 0–17: Child, 18–64: Adult, 65+: Senior

Income — 0–30k: Low, 30k–80k: Middle, 80k+: High

Aggregated Features

Customer Purchase History
  • Total purchases in last 30 days
  • Average order value
  • Number of product categories purchased

Aggregated features provide context that individual records alone cannot.

Domain Knowledge

Domain-Specific Features

Healthcare — BMI = weight / height², Blood pressure ratio, Symptom duration in days

Finance — Debt-to-income ratio, Loan-to-value ratio, Credit utilization rate

Education — Grade point average, Completion rate, Attempts per assignment

Feature Scaling

Why Scaling Matters

Feature 1 — Age, range 18–80

Feature 2 — Income, range 20,000–200,000

Without scaling the model may give more weight to income simply because its values are larger.

Fix — Apply normalization or standardization

Handling Missing Feature Values

Options for Missing Values

Customer income missing

  • Fill with mean (if no major outliers)
  • Fill with median (if outliers present)
  • Fill with mode for categorical
  • Create missing indicator: Age_missing = 1

A missing indicator feature tells the model that the fact that a value is missing may itself carry information.

Data Leakage in Feature Engineering

Student Final Exam Example

Predicting — Whether the student will pass the final exam

Leaked feature — "Final exam score" (this IS the thing you are predicting — it cannot be an input feature)

Including information that would not be available at prediction time is data leakage.

Overfitting Risk

Too Many Features

Training accuracy — Very high (e.g., 99%)

Test accuracy — Much lower (e.g., 72%)

Adding too many features can cause the model to memorize training data rather than learn patterns that generalize.

Feature Importance

House Price Model — Feature Importance

1. Location — Most important

2. House size — Very important

3. Number of bedrooms — Moderately important

4. Age of house — Less important

Real-World Feature Engineering Examples

House Price

Raw — Square footage, year built, zip code

Engineered — Age of house, price-per-sqft, distance to city center

Student Performance

Raw — Grades, attendance

Engineered — Average score trend, improvement rate, subject difficulty score

Recommendation System

Raw — User click history, product categories

Engineered — Click frequency, recency score, category preference ratio

Feature Engineering vs Feature Selection

Feature EngineeringFeature Selection
Creates new featuresChooses from existing features
Transforms raw dataRemoves redundant or unhelpful features
Requires domain knowledgeCan be automated (e.g., feature importance)
Happens before model trainingCan happen before or after training

Common Mistakes

Mistakes to Avoid
  • Creating features that leak future information
  • Creating too many redundant features
  • Not scaling features when required
  • Engineering features without domain knowledge
  • Including features not available at prediction time

Feature Engineering Process

01

Understand the problem and prediction target

02

Explore the raw data and understand each column

03

Identify relevant existing features

04

Create new features from existing ones

05

Apply transformations (log, sqrt, ratios, binning)

06

Encode categorical features

07

Scale numerical features

08

Remove redundant or low-value features

09

Check for data leakage

10

Evaluate model performance with engineered features

Key Takeaways

Summary
  • Feature engineering creates better inputs for machine learning models
  • Good features are relevant, available, reliable, and distinct
  • New features can be created through transformations, ratios, and aggregations
  • Domain knowledge helps identify the most useful features
  • Always check for data leakage in engineered features
  • Feature importance scores show which features helped the model most

Need Help?

Ask the AI if you need help understanding how to create features, which transformations to apply, how to encode categories, or how to avoid common feature engineering mistakes.