What is feature engineering?
Feature engineering is the process of converting raw data into useful inputs (features).
Examples:
- Date → day of week
- Amount → log(amount)
- Text → length, presence of keywords
- Customer transactions → total spend, avg order value
Example: derive features from datetime
import pandas as pd
df = pd.DataFrame({
"order_time": pd.to_datetime([
"2025-01-01 10:15:00",
"2025-01-02 18:30:00",
"2025-01-03 09:10:00",
]),
"amount": [250, 180, 90],
})
df["hour"] = df["order_time"].dt.hour
df["weekday"] = df["order_time"].dt.day_name()
df["is_weekend"] = df["order_time"].dt.weekday >= 5
print(df)Example: ratios and flags
import pandas as pd
df = pd.DataFrame({"revenue": [1000, 500], "users": [100, 20]})
df["revenue_per_user"] = df["revenue"] / df["users"]
df["is_high_value"] = df["revenue_per_user"] > 20
print(df)Example: aggregated features (groupby)
import pandas as pd
orders = pd.DataFrame({
"customer_id": [1, 1, 2, 2, 2],
"amount": [100, 250, 90, 180, 300],
})
cust = orders.groupby("customer_id").agg(
total_spend=("amount", "sum"),
avg_order=("amount", "mean"),
orders=("amount", "count"),
).reset_index()
print(cust)Guiding principles
- Don’t leak target/future information.
- Prefer simple features first.
- Validate with charts and summary stats.
- Document each feature and its meaning.
Visualize it
flowchart LR A["order_time (raw)"] --> B["Extract hour, weekday, is_weekend"] C["revenue, users (raw)"] --> D["Derive revenue_per_user ratio"] E["customer transactions (raw)"] --> F["Aggregate: total_spend, avg_order"] B --> G["Engineered features"] D --> G F --> G
🧪 Try It Yourself
Exercise 1 – Derive a feature with map
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Extract datetime parts
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Aggregate a feature per group
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
Which example turns a datetime into a useful feature?
Extracting hour, weekday or is_weekend from a datetime gives models something to learn from.
Show answer
A — df["order_time"].dt.hour — Extracting hour, weekday or is_weekend from a datetime gives models something to learn from.
With revenue 1000 and 500 and users 100 and 20, which rows have revenue_per_user greater than 20?
The ratios are 1000/100 = 10 and 500/20 = 25, so only the second row is flagged as high value.
Show answer
A — Only the second row, at 25 — The ratios are 1000/100 = 10 and 500/20 = 25, so only the second row is flagged as high value.
For customer 2 with orders 90, 180 and 300, what do total_spend, avg_order and orders come out as?
90 + 180 + 300 = 570, the mean is 190 and there are 3 orders.
Show answer
A — 570, 190.0 and 3 — 90 + 180 + 300 = 570, the mean is 190 and there are 3 orders.
Which guiding principle does the lesson give about the target?
Leaking the target or the future makes a model look great in testing but fail in real use.
Show answer
A — Do not leak target or future information into features — Leaking the target or the future makes a model look great in testing but fail in real use.
Next
You've now covered the whole phase — wrap it up by making sure raw columns have the right Data Type Conversion and Validation before any of these features are built.