Loading
0x10Lesson 2 of 11

Prepare data and features

Split data first, scale and encode features with training statistics only, and avoid leakage.

22 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • Split data into training, validation and test sets
  • Standardize numbers and one-hot encode categories
  • Spot and prevent data leakage

Models only understand numbers, and they learn best when features are on comparable scales. Two standard transformations:

  • Standardization: z=x−μσz = \frac{x - \mu}{\sigma}, using the mean μ\mu and standard deviation σ\sigma of the training data, so every feature is centered at 0 with spread 1.
  • One-hot encoding: a category with values red, green, blue becomes three 0/1 columns. Numbering them 1, 2, 3 instead would invent an order that doesn’t exist.

But first, split the data: typically 60-80% for training, some for validation (choosing settings), and a held-out test set touched only once at the end. Then fit every transformation on the training set alone and apply it unchanged to the others.

Try it

Up to 15 XP

Leak or no leak?

A bank predicts whether a loan will default. Which features or steps leak information the model wouldn’t have at prediction time?

0 of 5 sortedScore 0/0
  • “Applicant’s income at the time of application”

  • ““Collections agency contacted” flag”

  • “Standardizing income with the mean of the whole dataset, test set included”

  • “Number of previous loans repaid”

  • “Splitting randomly when the same customer appears in both training and test sets”

standardize.py
1import numpy as np
2train = np.array([10.0, 20.0, 30.0])
3mean, spread = train.mean(), train.std()
4print(np.round((train - mean) / spread, 3))
5print(round((40 - mean) / spread, 3))
Output
[-1.225  0.     1.225]
2.449

Key takeaways

  • Split first; touch the test set only once, at the end.

  • Standardize with training statistics; one-hot encode categories.

  • Leakage - future information or test statistics in training - makes scores lie.

Lesson quiz

7 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Python

Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.

Exercise 1

Standardize with training statistics

+25 XP

Line 1 holds training values, line 2 test values. Using numpy, compute the training mean and (population) standard deviation, then print mean=M std=S (3 decimals) and the standardized test values rounded to 3 decimals as a list (np.round(...).tolist()).

  • Incomes in thousands
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 2

One-hot encode a category

+25 XP

Line 1 lists training categories, line 2 test categories. The columns are the distinct training categories, sorted. Print the header columns: A, B, C, then one row of 0/1 values per test category (space-separated). A category never seen in training becomes all zeros.

  • Colors
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…