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
#memberagebranchplanvisitsspendjoined
0M0123CampusFree225.22025-09-03
1M0241DowntownPlus911.82024-02-17
2M0335AirportFree214.52026-01-22
3M0429DowntownFree156.92025-05-30
4M0558RiversidePlus68.12023-11-09
5M0619CampusFree304.42025-10-12
6M0746RiversideFree4292024-08-01
7M0833AirportPlus119.62025-03-14

8 rows × 7 columns

Models only understand numbers. Branch and plan are text, joined is a date, and visits and spend are on very different scales.

Categories

MethodWhat it doesUse when
One-hotOne 0/1 column per category: branch_Downtown, branch_Riverside, …Categories with no order (branch, payment type). The safe default.
OrdinalMap to 0, 1, 2… in a meaningful orderOrdered categories (size S/M/L, plan Free/Plus/Pro).
Frequency or target encodingReplace a category by how common it is, or by the average target for itMany 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

Question 1

1.Why not code four branches as 1, 2, 3, 4?

Question 2

2.Which model needs scaled features most?

Question 3

3.Spend has a long right tail. A common transform is…

Quiz score: 0 of 3

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

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.