Module 10 · Feature Engineering: Making Data Model-Ready · Lesson 32 of 51
From columns to features
What a feature is, one-hot and ordinal encoding for categories, scaling, log transforms and bins for numbers, and useful features from dates and ratios.
In this lesson · about 40 minutes
- Explain why models need numbers and what a good feature is
- Encode categories with one-hot or ordinal encoding, and pick the right one
- Scale, log-transform and bin numeric columns, and build date and ratio features
Try it · data lab
Feature engineering workbench
Pick a column and a transform, and watch the raw values turn into numbers a model can learn from.
pandas
members
| # | member | age | branch | plan | visits | spend | joined |
|---|---|---|---|---|---|---|---|
| 0 | M01 | 23 | Campus | Free | 22 | 5.2 | 2025-09-03 |
| 1 | M02 | 41 | Downtown | Plus | 9 | 11.8 | 2024-02-17 |
| 2 | M03 | 35 | Airport | Free | 2 | 14.5 | 2026-01-22 |
| 3 | M04 | 29 | Downtown | Free | 15 | 6.9 | 2025-05-30 |
| 4 | M05 | 58 | Riverside | Plus | 6 | 8.1 | 2023-11-09 |
| 5 | M06 | 19 | Campus | Free | 30 | 4.4 | 2025-10-12 |
| 6 | M07 | 46 | Riverside | Free | 4 | 29 | 2024-08-01 |
| 7 | M08 | 33 | Airport | Plus | 11 | 9.6 | 2025-03-14 |
8 rows × 7 columns
Categories
| Method | What it does | Use when |
|---|---|---|
| One-hot | One 0/1 column per category: branch_Downtown, branch_Riverside, … | Categories with no order (branch, payment type). The safe default. |
| Ordinal | Map to 0, 1, 2… in a meaningful order | Ordered categories (size S/M/L, plan Free/Plus/Pro). |
| Frequency or target encoding | Replace a category by how common it is, or by the average target for it | Many categories (thousands of postcodes). Target encoding must be computed on training data only. |
Why not just number the branches 1–4? Because the model would think Riverside (2) is "between" Downtown (1) and Station (3), which means nothing.
Numbers
- Standardise (z-score): (x − mean) ÷ std. Puts features on the same scale; needed for distance-based models (k-means, k-nearest neighbours) and regularised linear models.
- Min-max: (x − min) ÷ (max − min), into 0–1.
- Log:
np.log1p(x)tames long right tails (spend, income, counts). - Bins: cut ages into 18–29, 30–44, 45+. Loses detail, but can capture non-linear patterns for simple models and is easy to explain.
Tree models (decision trees, random forests, gradient boosting) don't care about scale; linear models and anything using distances do.
Dates and ratios
A raw date is rarely useful. Extract day of week, month, is weekend, days since last order, tenure. Combine columns into ratios that carry meaning: spend per visit, complaints per month of tenure, share of orders paid in the app.
Encoding and transforms with pandas
Exercise
One-hot the branches
Make X: the columns age, visits_90d and avg_spend, plus one-hot columns for home_branch with the prefix branch (integers 0/1). Then set n_cols to the number of columns in X and downtown to how many members have branch_Downtown == 1.
Code that runs before yours
import pandas as pd
import numpy as np
members = pd.read_csv("kc_members.csv")Exercise
Ratios and bands
Add complaints_per_year = complaints ÷ (tenure_months ÷ 12), using tenure_months.clip(lower=1) so nobody divides by zero. Then set churn_by_band to the churn rate (rounded to 3 decimals) for members with complaints_per_year above 1 and at most 1, as a tuple (above, at_most).
Code that runs before yours
import pandas as pd
import numpy as np
members = pd.read_csv("kc_members.csv")Sorting drill
Which encoding?
Pick the best way to turn each column into numbers.
- Payment type: card, cash, app
- Cup size: small, medium, large
- Order timestamp
- Home branch
- Satisfaction: poor, fair, good, great
- Total spend and number of visits
0 of 6 sorted
Check your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 2
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.