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.
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
| Issue | What to do |
|---|---|
| Missing features | Few affected examples: drop them. Otherwise impute with the training mean or median and add a "was missing" indicator |
| Categorical inputs | One-hot encode. An integer code (red = 1, green = 2, blue = 3) invents an order and spacing |
| Noisy labels | Labels are expensive and often wrong; inspect a sample by hand |
| Unbalanced classes | If 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 category | Example | Use |
|---|---|---|
| No order | colour, city | One-hot |
| Real order | small < medium < large | Plain numbers 0, 1, 2 |
| Thousands of categories | product IDs | Embeddings (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
Data checks
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.