Lesson 8.4 · 7 min read · about 50 min to master at a very slow pace

Preparing the Data

Scaling features, handling missing values and categories, checking labels and dealing with unbalanced classes. Each with the reason it matters for a neural network.

Not startedJump to quiz

Scale the features

If one feature lies in and another in , the loss surface is badly conditioned (gradient descent zig-zags) and large inputs push sigmoid units into saturation. Rescale each feature :

Compute on the training set only and apply the same transformation to validation, test and future inputs. Images: divide pixel values by 255. Standardise regression targets too.

From the lab

Inputs in saturated the sigmoid hidden units and training stalled. Standardising them (training mean and standard deviation) fixed it. Always do this.

Other checks

IssueWhat to do
Missing featuresFew affected examples: drop them. Otherwise impute with the training mean or median and add a "was missing" indicator
Categorical inputsOne-hot encode. An integer code (red = 1, green = 2, blue = 3) invents an order and spacing
Noisy labelsLabels are expensive and often wrong; inspect a sample by hand
Unbalanced classesIf 99% of examples are one class, predicting it always scores 99% accuracy. Use a confusion matrix, precision and recall; re-weight the loss or over-sample the rare class

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passMin–max or standardisation: which should I use?

Both work for most networks. Standardisation is the common default and copes better with outliers (one extreme value squashes every other value under min–max). Min–max is natural when the range is known and fixed, such as pixel values from 0 to 255.

First passWhen do I use one-hot encoding?

When a feature is a category with no natural order: colour, city, blood type. Coding red = 1, green = 2, blue = 3 would tell the network that blue > green > red, an order that isn't true. One-hot gives each category its own 0/1 column, so all categories are equally far apart.

Kind of categoryExampleUse
No ordercolour, cityOne-hot
Real ordersmall < medium < largePlain numbers 0, 1, 2
Thousands of categoriesproduct IDsEmbeddings (lesson 11.2)

Class labels for multi-class outputs are one-hot too: cat = (1, 0, 0), compared with the softmax output by cross-entropy.

Thumb rule: "No order → one-hot. Real order → numbers."

First passWhat are precision, recall and F1, and how do I remember them?
  • Precision → Predicted: of everything the model predicted positive, how many really are?
  • Recall → Real: of everything that really is positive, how many did the model find?
  • F1 combines them: . It's low if either is low, so a model can't score well by being good at only one.

Example: there are 10 real positives; the model predicts 8 positives, of which 6 are right. Precision , recall , .

Use them instead of accuracy when the classes are unbalanced.

Cheatsheet

Scaling

Min–max ; standardise . Statistics from training data only. Pixels ÷ 255.

Data checks

Missing: drop or impute + indicator. Categories: one-hot. Labels: inspect. Imbalance: confusion matrix, precision/recall, re-weight.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateTraining data for a feature range from 20 to 70. What is the min–max scaled value of 45?
Q2applyYou standardise using the mean and standard deviation of the whole dataset before splitting. What's the problem?
Q3conceptWhy do unscaled inputs in the thousands cause trouble for sigmoid hidden units?
Q4applyA feature "colour" takes values red, green and blue. How should you feed it to the network?
Q5interpret99% of transactions are legitimate. Your fraud model has 99% accuracy. What should you check next?