Feature Engineering
Beginner 10 min read Feature Engineering

Feature Engineering Introduction

The craft that wins most competitions

Feature engineering is the deliberate transformation of raw data into features that make model learning easier — it is where most real-world ML performance is won.

feature engineeringfeaturestransformationsmodel performancedata preparation
Prerequisites: Pandas Introduction
Before we start

What you will learn

  • Explain why feature engineering matters more than algorithm choice at the margin
  • Classify engineering moves: cleaning, encoding, scaling, construction, selection
  • Run a structured feature build loop: idea -> corpus evidence -> validate on held-out data
Introduction

What is this?

Feature engineering is the process of transforming raw, messy data into a curated feature matrix that raises the signal-to-noise ratio a model has to work with. It covers fixing values (missingness, outliers), representing types (encodings, datetimes), rescaling (normalizing, standardizing), and crafting new columns that expose relationships the raw data only implies.

Motivation

Why do you need it?

  • Algorithms plateau; features are the last big lever available to a team.
  • Models cannot learn what's not in the columns — daylight savings, holidays, lagged demand stay invisible unless you encode them.
  • Good features shrink the model size and training cost needed for the same accuracy.
  • Encoded, scaled, complete features make the model's learned pattern interpretable.
Think about it

Mental model

Imagine

Preparing ingredients before cooking

Think of

Engineering features before training

A great chef with dull ingredients makes a mediocre meal; a great dish starts at the market. Garbage-in columns force the model to be a detective instead of a chef.

Mechanics

How it works

  • Start with the data audit: dtypes, missingness, domain units, range sanity.
  • Fix value-level noise: impute or flag missing, treat outliers on evidence, dedupe rows.
  • Represent semantics: one-hot categories, ordinal ranks, datetime components, frequency encodings.
  • Rescale numerics so gradient-based models train stably.
  • Construct candidate features; each one earns its place by beating a no-feature baseline on validation.
  • Select features: remove constant, correlated, and low-information columns.

The feature engineering pipeline.

Audit
Clean
Represent
Rescale
Construct
Select
Hands-on

Examples

A minimal engineered feature build

A minimal engineered feature build
import pandas as pd
import numpy as np
df = pd.read_csv("orders.csv", parse_dates=["order_ts"])
df["order_hour"] = df["order_ts"].dt.hour
df["is_weekend"] = df["order_ts"].dt.dayofweek.isin([5, 6]).astype(int)
df["amount_per_item"] = df["total_amount"] / df["item_count"].replace(0, np.nan)
df["item_count_missing"] = df["item_count"].isna().astype(int)
df["total_amount"] = df["total_amount"].fillna(df["total_amount"].median())
features = df[["order_hour", "is_weekend", "amount_per_item", "item_count_missing"]]
print(features.head())
print(features.isna().sum())

Each new column exposes something the raw frame hides: the time-of-day cycle, weekend signal, per-item economics, and an explicit missingness flag instead of a silent NaN.

Real world

In production

  • Taxi demand: hour-of-day, weather indicator, airport/nearby-events flags added 15+ points of RMSE improvement over raw timestamps.
  • Fraud: ratios of amounts, velocity (count in last hour), and merchant-category interactions dominate raw fields.
  • Recommendation: user x item interaction features beat pure content/user/item embeddings in shallow models.
Guidelines

Best practices

  • One transformation per new column; never derive features from the target; derive train/test features with the SAME fitted encoders/scalers; keep a feature manifest so production predicts on identical columns.
Watch out

Common mistakes

  • Fitting scalers/encoders on the whole dataset (leakage); applying datetime parts in the wrong timezone; engineering features after cross-validation splits; dropping outliers without evidence; letting correlated engineered features double-count signal.
When things break

Debugging tips

  • Check train/test feature columns are identical (same order, same dtype) before fitting.
  • Sanity-test feature code against a tiny hand-labeled DataFrame.
  • Log a single row's feature values train vs production to catch pipeline drift.
  • If a feature helps train but not validation, suspect target leakage.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

Why is fitting a StandardScaler on the full dataset before CV a leak?

2

A feature that helps train accuracy but not validation accuracy is a symptom of:

Go deeper

Official documentation