Data Science · Chapter 22 of 43

Feature Engineering

FEATURE ENGINEERING is creating better inputs for your model from raw data — ratios, log transforms, date parts, text lengths, aggregations.

Often more impactful than switching algorithms.

Example 1 (python)
import pandas as pd
df = pd.read_csv('sales.csv')
df['order_date'] = pd.to_datetime(df['order_date'])
df['month'] = df['order_date'].dt.month

Extract month from a date.

Example 2 (python)
df['price_per_unit'] = df['total'] / df['units']

Domain-driven ratio.

Key points

  • Better features often beat fancier models.
  • Common: dates → parts, counts, ratios, logs.
  • Use domain knowledge.
  • Careful with features that leak the target.
💡 Note: Guarding against target leakage is critical — a feature computed 'from the future' will look magical in offline tests and disappoint in production.

📝 Quick Quiz

1. Feature engineering usually:

2. Extracting month from a date is:

3. Features that use future information cause: