Chap 1 - Introduction
What is Machine Learning?
Definition by Arthur Samuel(1959): “Field of study that gives computers the ability to learn without being explicitly programmed.”
Definition by Tom Mitchell(1998): “A computer program is said to learn form experience E with respect to some task T and some performance measure P, if its performance on T, as measured by P, improves with experience E.”
In general, any ML problem can be assigned to one of two broad classification: supervised learning or unsupervised learning.
- Supervised learning is the ML task of learning a way to map input signals to output values by training a model on a
set of training examples where each example is a pair consisting of an input and a desired output value.
- Alternatively, we know the relationship between the input and output.
- Regression Problem: try to predict results within a continuous output (ie, map input variables to some continuous function)
- E.g. House Price Prediction
- Classification Problem: try to predict results in a discrete output (ie, map input variables into discrete categories)
- E.g. Cancer Categorization (malignant/benign tumor), Email spam/not spam.
- Unsupervised ML is the ML task of inferring a function to describe hidden structure from “unlabeled” data.
Examples of unsupervised learning tasks include clustering (where we try to discover underlying groupings of examples)
and anomaly detection (where we try to infer if some examples in a dataset do not conform to some expected pattern).
- Alternatively, we have little or no idea what our results should look like.
- There is no feedback based on the prediction results with unsupervised learning.
- E.g.: Orgnize computing clusters, Social network analysis, Market segmentation, Astronomical data analysis
- Clustering Problem Example: Automatically group a huge amount of genes into groups that are somehow similar or related by different variables, such as lifespan, location, roles, and so on.
- Non-clustering Problem Example: The “Cocktail Party Algorithm”, allows you to find structure in a chaotic environment.
Model and Cost Function
In regression problems, we are taking input variables and trying to fit the output onto a continuous expected result function.
Linear regression with one variable is also known as “univariate linear regression.”
Univariate linear regression is used when predicts a single output value y from a single input value x.
- = number of training examples
- = “input” variable / features
- = “output” variable / “target” variable
- = one training example
- training example
- parameter of model
Note that this is like the equation of a straight line. We give to values for and to get our estimated output . In other words, we are trying to create a function called that is trying to map our input data (the ) to the output data (the ))
The accuracy of hypothesis function can be measured by a cost function. This takes an “average” of all the results of the hypothesis with inputs from compared to the actual output .
This function is also known as “Squared Error Function”. - Please refer to Squared Error Function (aka, Mean squared error)
After having a hypothesis function and a cost function to measure hypothesis function, now we uses Gradient Descent to estimate the parameters in hypothesis function.
Imagine that we graph our hypothesis function based on its fields and (actually we are graphing the cost function as a function of the parameter estimates). This can be kind of confusing; we are moving up to a higher level of abstraction. We are not graphing x and y itself, but the parameter range of our hypothesis function and the cost resulting from selecting particular set of parameters.
We put on the x axis and on the y axis, with the cost function on the vertical z axis. The points on our graph will be the result of the cost function using our hypothesis with those specific theta parameters.
We will know that we have succeeded when our cost function is at the very bottom of the pits in our graph, i.e. when its value is the minimum.
The way we do this is by taking the derivative (the tangential line to a function) of our cost function. The slope of the tangent is the derivative at that point and it will give us a direction to move towards. We make steps down the cost function in the direction with the steepest descent, and the size of each step is determined by the parameter , which is called the learning rate.
The gradient descent algorithm is:
Gradient Descent for Linear Regression
- “Batch” Gradient Descent: Each step of gradient descent uses all the training examples.
- Gradient descent can converge even if is kept fixed. (But cannot be too large, or else it may fail to converge).
- For the specific choice of cost function J used in linear regression, there are no local optima (other than the global optimum).
Chap 2 - Linear Regression with Multiple Variables
Linear regression with multiple variables is also known as “multivariate linear regression”.
We now introduce notation for equations where we can have any number of input variables.
Now define the multivariable form of the hypothesis function as follows, accommodating these multiple features:
In order to develop intuition about this function, we can think about as the basic price of a house, as the price per square meter, as the price per floor, etc. will be the number of square meters in the house, the number of floors, etc.
Using the definition of matrix multiplication, our multivariable hypothesis function can be concisely represented as:
Remark: Note that for convenience reasons in this course Mr. Ng assumes
The training examples are stored in X row-wise, like such:
You can calculate the hypothesis as a column vector of size (m x 1) with:
For the rest of these notes, will be represent a matrix of training examples stored row-wise.
For the parameter (of type or ),
The vectorized version:
Gradient Descent for Multiple Variables
The gradient descent equation itself is generally the same form; we just have to repeat it for our ‘n’ features:
In other words:
The Gradient Descent rule can be expressed as:
where is a vector of the form:
The j-th component of the gradient is the summation of the product of two terms:
Sometimes, the summation of the product of two terms can be expressed as the product of two vectors.
Here, for i = 1, 2, … m, represents the m elements of the j-th columns, , of the training set X.
The other term is the vector of the deviation between the prediction and the true values . Rewriting , we have:
The vectorized matrix notation:
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:
−1 ≤ ≤ 1 or −0.5 ≤ ≤ 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:
Where is the average of all the values for feature (i) and is the range of values (max - min), or 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.
Example: xi is housing prices with range of 100 to 2000, with a mean value of 1000. Then,
Gradient Descent Tips
- Debugging gradient descent: Make a plot with number of iterations on the x-axis. Now plot the cost function, over the number of iterations of gradient descent. If ever increases, then you probably need to decrease .
- Automatic convergence test: Declare convergence if decreases by less than E in one iteration, where E is some small value such as . However in practice it’s difficult to choose this threshold value.
It has been proven that if learning rate is sufficiently small, then will decrease on every iteration. Andrew Ng recommends decreasing by multiples of 3.
Features and Polynomial Regression
We can improve our features and the form of our hypothesis function in a couple different ways.
We can combine multiple features into one. For example, we can combine and into a new feature by taking .
Polynomial Regression Our hypothesis function need not be linear (a straight line) if that does not fit the data well.
We can change the behavior or curve of our hypothesis function by making it a quadratic, cubic or square root function (or any other form).
For example, if our hypothesis function is then we can create additional features based on , to get the quadratic function or the cubic function . In the cubic version, we have created new features and .
To make it a square root function, we could do:
One important thing to keep in mind is, if you choose your features this way then feature scaling becomes very important.
eg. if has range 1 - 1000 then range of becomes 1 - 1000000 and that of becomes 1 - 1000000000.
The “Normal Equation” is a method of finding the optimum theta without iteration:
The following is a comparison of gradient descent and the normal equation:
|Gradient Descent||Normal Equation|
|Need to choose||No need to choose|
|Need many iterations||No need to iterate|
|, need to calculate inverse of|
|Works well when n is large||slow if n is large|
Normal Equation Non-invertibility
When implementing the normal equation in octave we want to use the ‘pinv’ function rather than ‘inv.’ may be non-invertible. The common causes are:
- Redundant features, where two features are very closely related (i.e. they are linearly dependent)
- Too many features (e.g. ). In this case, delete some features or use “regularization” (to be explained in a later lesson).