Feature Engineering

Create new, more informative inputs from existing columns to help the model.

Feature engineering is inventing better inputs from raw data. A good feature can matter more than a fancy model.

Typical ideas

  • Combine columns: price_per_sqm = price / area.
  • Extract parts of a date: month, weekday, is_weekend.
  • Bucket a number into ranges (age → age group).
  • Take logs to tame skewed values.

Example

Example · python
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'price': [200000, 350000],
    'area':  [80, 100],
    'date':  pd.to_datetime(['2026-01-05', '2026-07-11']),
})

df['price_per_sqm'] = df['price'] / df['area']
df['month'] = df['date'].dt.month
df['log_price'] = np.log(df['price'])
print(df[['price_per_sqm', 'month', 'log_price']])

When to use it

  • A data scientist creates an 'age at purchase' feature by subtracting customer birthdate from transaction date, revealing a strong predictor not present in the raw data.
  • An ML engineer adds polynomial interaction terms for two correlated features to let a linear model capture their non-linear combined effect.
  • A fraud-detection team engineers a 'transactions per hour' rate feature from raw timestamp and count columns, which becomes the model's most important predictor.

More examples

Date-derived features

Derives two new features from raw datetime columns: the number of days the user has been a member and the month they signed up.

Example · python
import pandas as pd

df = pd.DataFrame({'signup_date': pd.to_datetime(['2022-01-15', '2023-06-01', '2021-11-20']),
                   'purchase_date': pd.to_datetime(['2024-03-10', '2024-03-10', '2024-03-10'])})

df['days_as_member'] = (df['purchase_date'] - df['signup_date']).dt.days
df['signup_month']   = df['signup_date'].dt.month
print(df[['days_as_member', 'signup_month']])

Ratio and log-transform features

Creates a debt-to-income ratio and a log-transformed income feature, both common transformations that help models handle skewed financial data.

Example · python
import pandas as pd
import numpy as np

df = pd.DataFrame({'income': [30000, 80000, 150000],
                   'debt':   [5000,  20000,  8000]})

df['debt_to_income'] = df['debt'] / df['income']
df['log_income']     = np.log1p(df['income'])  # log1p handles 0 safely
print(df)

Polynomial features with scikit-learn

Expands two features into degree-2 polynomial terms including squared and interaction terms, useful for fitting non-linear boundaries with linear models.

Example · python
import numpy as np
from sklearn.preprocessing import PolynomialFeatures

X = np.array([[2, 3], [4, 1], [1, 5]])
poly = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly.fit_transform(X)
print('Original shape:', X.shape)
print('Poly shape:', X_poly.shape)
print(poly.get_feature_names_out(['a', 'b']))

Discussion

  • Be the first to comment on this lesson.