Loading [MathJax]/jax/output/HTML-CSS/jax.js
본문 바로가기

BITS

[Machine Learning by Stanford] Gradient Descent in Practice I - Feature Scaling

This is a brief summary of ML course provided by Andrew Ng and Stanford in Coursera.

You can find the lecture video and additional materials in 

https://www.coursera.org/learn/machine-learning/home/welcome

 

Coursera | Online Courses From Top Universities. Join for Free

1000+ courses from schools like Stanford and Yale - no application required. Build career skills in data science, computer science, business, and more.

www.coursera.org

Feature Scaling

Idea: Make sure feautres are on a similiar scale. 

e.g., x1 = size (0 - 2000 ft2x2 = number of bedrooms (1-5)

It can take a long time to find the way to the global minimum, if you run gradient descents on this funcion. 

 

x1=fracsize2000,x2=fracnumofbedrooms5

But if you scale as above, you can find a much more direct path to the global minimum rather than taking a much more convoluted path where you are sorty of trying to follow a much more complicated trajectory to get to the global min. 

Get every feature into approximately a 1xi1 range.

Mean Normalization

Replace xi with xiμi to make features have approximately zero mean (Do not apply to x0 = 1).

e.g., x1=size10002000x2=numofbedrooms25

-> 0.5x10,5,0.5x20.5 

 

Quiz: Suppose you are using a learning algorithm to estimate the price of houses in a city. You want one of your features xi to capture the age of the house. In your training set, all of your houses have an age between 30 and 50 years, with an average age of 38 years. Which of the following would you use as features, assuming you use feature scaling and mean normalization?

 

Answer: ageofhouse3820

 

Lecturer's Note

We can speed up gradient descent by having each of our input values in roughly the same range. This is because θ will descend quickly on small ranges and slowly on large ranges, and so will oscillate inefficiently down to the optimum when the variables are very uneven.

The way to prevent this is to modify the ranges of our input variables so that they are all roughly the same. Ideally:

1x(i)1

0.5x(i)0.5

 

These aren't exact requirements; we are only trying to speed things up. The goal is to get all input variables into roughly one of these ranges, give or take a few.

Two techniques to help with this are feature scaling and mean normalization. Feature scaling involves dividing the input values by the range (i.e. the maximum value minus the minimum value) of the input variable, resulting in a new range of just 1. Mean normalization involves subtracting the average value for an input variable from the values for that input variable resulting in a new average value for the input variable of just zero. To implement both of these techniques, adjust your input values as shown in this formula:

 

xi:=xiμisi

where μi is the average of all the values for feature (i) and si is the range of values (max - min) or si is the standard deviation. 

 

Note that dividing by the range, or dividing by the standard deviation, give different results. The quizzes in this course use range - the programming exercises use standard deviation.

 

For example, if xi represents housing prices with a range of 100 to 2000 and a mean value of 1000, then, xi:=price10001900