Feature scaling rescales your features so they all sit in comparable ranges (roughly −1 to +1). This reshapes the cost function so gradient descent can take a much more direct path to the global minimum, and it runs faster. To check that gradient descent is converging, plot the cost J against the number of iterations. It should go down on every iteration and eventually flatten out. If it ever goes up, your learning rate α is probably too large, or there’s a bug in your code.
GeekCoding101 · Apr 25, 2024 · Topics: notes, supervised-machine-learning, machine-learning
Welcome back
I didn’t get much time for the course over the past 5 days, but I’m finally resuming today!
Today I reviewed Feature scaling part 1 and worked through Feature scaling part 2 and Checking gradient descent for convergence.
The course is getting harder. The videos are around 20 minutes, but I spent double that time on them and needed to read some external articles to really understand what was going on.
Feature scaling
Features vs. parameters
First, let’s be clear about what we’re scaling. Take this model for predicting a house price:
price_hat = w1·x1 + w2·x2 + b- x1 and x2 are features. Here x1 is the size of the house and x2 is the number of bedrooms.
- w1, w2 and b are parameters. These are the values the model learns.
When a feature has a large range of possible values, like house size, a good model tends to learn a relatively small parameter for it. When a feature has a small range, like number of bedrooms, a reasonable parameter for it tends to be relatively large, like 50.
Why scaling matters for gradient descent
Suppose one feature spans thousands and another spans single digits. Then the contour plot of the cost function J(w, b) turns into tall, skinny ellipses. Gradient descent ends up bouncing back and forth across the narrow direction and crawls toward the minimum.
Andrew’s point at the end of part 1 is that you should rescale (transform) the features first. After that, the cost function built on the transformed data is much more evenly shaped, and gradient descent can find a much more direct path to the global minimum.
In short: when different features take on very different ranges of values, gradient descent can run slowly. Rescaling them so they all take on a comparable range of values can speed up gradient descent significantly. (paraphrasing Andrew Ng)
Feature scaling is one part of a bigger topic in feature engineering: scaling, normalization and standardization. These techniques transform the data to make it more suitable for modeling. They can help improve model performance, reduce the impact of outliers, and put all the data on the same scale. I found Feature Scaling: Engineering, Normalization, and Standardization (Updated 2024) really helpful here (see References).
Three ways to scale a feature
Part 2 of the video covered three methods. Here they are side by side. μ is the mean of the feature, σ is its standard deviation, and max/min are its largest and smallest values in the training set.
| Method | Formula | Typical result |
|---|---|---|
| Divide by the maximum | x_scaled = x / max | Values between 0 and 1 (for non-negative features) |
| Mean normalization | x_scaled = (x − μ) / (max − min) | Values centered around 0, roughly −1 to +1 |
| Z-score normalization | x_scaled = (x − μ) / σ | Mean 0, standard deviation 1 |
A quick illustration: say house size runs from 300 to 2,000 and bedrooms from 0 to 5. Dividing size by 2,000 and bedrooms by 5 puts both features between 0 and 1, so neither one dominates the shape of J. The other two methods do the same job, but also center the values around zero.
One thing to remember: if you scale your training data, apply the same μ, σ (or max/min) to any new data you predict on.
How much scaling is enough?
Andrew gave a loose rule of thumb that I liked:
As a rule of thumb, when performing feature scaling, you might want to aim for getting the features to range from maybe anywhere around negative one to somewhere around plus one for each feature x. But these values, negative one and plus one can be a little bit loose. If the features range from negative three to plus three or negative 0.3 to plus 0.3, all of these are completely okay. If you have a feature x_1 that winds up being between zero and three, that’s not a problem. You can re-scale it if you want, but if you don’t re-scale it, it should work okay too. — Andrew Ng
My takeaway: if a feature’s range is much too large or much too small compared to the others, rescale it. If it’s already in a similar ballpark, scaling won’t hurt, but it isn’t required.
Checking gradient descent for convergence
Use a learning curve
The main tool here is the learning curve: a plot of the cost J(w, b) on the y-axis against the number of gradient descent iterations on the x-axis.
How to read it:
- J should decrease after every iteration. That’s what a working gradient descent looks like.
- If J ever increases after an iteration, either α (the learning rate) was chosen poorly, which usually means it’s too large, or there’s a bug in the code.
- When the curve flattens out, gradient descent has more or less converged, because J is no longer decreasing.
It’s hard to tell in advance how many iterations gradient descent will need. It might be dozens or many thousands, depending on the problem. That’s exactly why the learning curve is useful.
Automatic convergence test
The other option is an automatic convergence test. You pick a small number ε (epsilon), for example 0.001. If J decreases by less than ε in one iteration, you’re probably on the flat part of the curve, and you can declare convergence.
Andrew said he usually found choosing the right ε pretty difficult. He tends to look at the learning curve instead of relying on an automatic test. I’m planning to do the same: plot first, and maybe add an ε check as a backup.
| Approach | How it works | Pros / cons |
|---|---|---|
| Learning curve | Plot J vs. iterations and look for a steady drop that flattens out | Easy to spot problems (J going up); needs you to look at it |
| Automatic convergence test | Stop when J drops by less than ε in one iteration | Hands-off, but picking a good ε is tricky |
Terminology
| Term | Notes |
|---|---|
| Feature | An input variable to the model, e.g. house size (x1) or number of bedrooms (x2). |
| Parameter | A value the model learns, e.g. w1, w2, b. |
| Feature scaling | Transforming features so they take on comparable ranges of values. |
| Mean normalization | Subtract the mean, then divide by (max − min). |
| Z-score normalization | Subtract the mean, then divide by the standard deviation σ. |
| Standard deviation σ | A measure of how spread out the data is around the mean. A small σ means the data is clustered tightly around the mean; a large σ means it’s more spread out. If you’ve heard of the normal distribution (the bell-shaped or Gaussian curve), σ describes how wide that bell is. |
| Learning rate α | The step size gradient descent takes on each iteration. Too large and J can go up instead of down. |
| Learning curve | A plot of the cost J against the number of iterations, used to see whether gradient descent is converging. |
| Automatic convergence test | Declare convergence when J decreases by less than a small number ε in one iteration. |
FAQ
Why does feature scaling make gradient descent faster? Features with very different ranges stretch the cost function into long, narrow contours, and gradient descent zig-zags across them. After scaling, the contours are much more even, so gradient descent heads more directly to the minimum.
What’s the difference between mean normalization and z-score normalization? Both subtract the mean. Mean normalization then divides by the range (max − min). Z-score normalization divides by the standard deviation σ, which gives the feature a mean of 0 and a standard deviation of 1.
Do I always have to scale my features? No. Andrew’s rule of thumb is that ranges like −3 to +3 or −0.3 to +0.3 are fine, and a feature between 0 and 3 is okay as is. Rescale when a feature’s range is much larger or much smaller than the others.
How do I know gradient descent has converged? Plot the learning curve (J vs. iterations). Once the curve flattens out and J barely changes, it has more or less converged. You can also use an automatic test that stops when J drops by less than ε in one iteration.
What does it mean if the cost J goes up? Usually the learning rate α is too large. It can also mean there’s a bug in your gradient descent code. With a correct implementation and a small enough α, J should decrease on every iteration.
References
- Feature Scaling: Engineering, Normalization, and Standardization (Updated 2024)
Thanks for reading my blog post! Feel free to check out my other posts, or contact me via the social links in the footer.
