# Coursera machine learning memo

This holiday break, I somehow got into binge watching Coursera's Stanford Machine Learning course taught by Andrew Ng. I remember machine learning to be really math heavy, but I found this one more accessible.

Here are some notes for my own use. (I am removing all the fun examples, and making it dry, so if you're interested in machine learning, you should check out the course or its official notes.)

### Intro

Machine learning splits into supervised learning and unsupervised learning.

Supervised learning problems split to *regression* problem (predict real valued output) and *classification* problem (predict discrete valued output).

Unsupervised learning problems are the ones we don't have prior data of right or wrong. An example of unsupervised learning problem is clustering (find grouping among data). Another example is *Cocktail Party algorithm* (identify indivisual voices given multiple recordings at a party).

### Linear regression with one variable

#### Model representation

*x ^{(i)}* denotes

*input variables*(feature vector), and

*y*denotes the output (target variable). A pair

^{(i)}*(x*is called a

^{(i)}, y^{(i)})*training example*. A list of

*1 .. m*training example is called a

*training set*.

Supervised learning can be modeled as a process of coming up with a good function *h* called *hypothesis* that predicts *y* from an unknown *x*.

For instance, given square footage, predict price of a house in some city. In the above, *h _{θ}(x)* is modeled as a linear function. The hypothesis

*h*is called

_{θ}(x) = θ_{0}+ θ_{1}x*linear regression with one variable*(or univariate linear regression).

These coefficients *θ _{0}* and

*θ*are called

_{1}*parameters*(some textbooks call this

**w**for weight vector).

#### Cost function

Basically figuring out the parameters *θ* vector is the name of the game.

We can turn this into an optimization problem that minimizes the *cost function J(θ)*, which in this case is a mean squared error.

Let's say a hypothesis is *h _{θ}(x) = θ_{0} x*. Depending on the value we put into

*θ*, it could take different slopes as shown below. To get the intuition for the curve fitting, we can plot the cost function

_{0}*J(θ)*against

*θ*.

This makes it clear that there's a single minimum at *θ _{0} = 1*.

For two parameter case it might look like a 3D bowl (or more complicated).

For some fixed set of *θ _{0}, θ_{1}*,

*h*is a function of

_{θ}(x)*x*, and the cost function captures the error between the training examples and the hypothesis.

For two-parameter case, we can try to visualize *J(θ _{0}, θ_{1})* using a contour plot, smaller circles showing local maxima/minima.

#### Gradient descent

Given a hypothesis and its cost function, we can try to find the parameters that fits the data. One of the methods is gradient descent. In gradient descent, the parameters *θ _{0}, θ_{1}* are incrementally changed to lower the cost. It turns out that we can determine the direction of walking by taking the partial derivative of

*J(θ)*by

*θ*and multipiled by some learning rate

_{j}*α*(

*η*eta is used in textbooks).

Picking the right *α* matters here, since if it's too large you might end up overshooting the target.

#### Gradient descent for linear regression

Linear regression with one variale is modeled using the hypothesis:

*h _{θ}(x) = θ_{0} + θ_{1} x*

The partial derivative of the cost function looks like this:

#### Linear algebra

Skipping the notes on linear algebra section.

- Learn how to multiply matrices and vectors.

### Multivariate linear regression

We can expand on the model *h _{θ}(x)* to incorporate multiple variables. For example, instead of just the size of a house, we can number of bedrooms, age etc.

Using vectors *θ* and *x*, we can seamlessly build up on dimensions. The trick to express the whole thing as a vector multiplication is to hardcode *x _{0}* to 1.

#### Gradient descent for multivariate linear regression

Here's how we can find the parameters *θ* using gradient descent.

Note that *x _{0} = 1*.

#### Feature scaling

When using gradient descent, it's important get all the features in the similar scale so the algorithm won't overshoot. The convention is to get them in the neighborhood of -1 to 1 range, but -3 ~ 3 is considered ok.

#### Picking the learning rate

If the learning rate *α* is too small, it's slow to converge. If it's too large, it may overshoot and not converge.

To debug this, it's recommended to plot cost function *J(θ)* against the number of iterations, and see if it's dropping fast enough. The suggested increments for *α* are 0.001, 0.003, 0.01, 0.03, ...

#### Polynomial regression

We can further expand the model *h _{θ}(x)* for polynomial function. The linear boundaries are straight lines and planes, whereas polynomial can express more complex curves and shaples like ellipsoid.

The amazing thing about is that as far as the learning algorithm is concerned, it doesn't really change anything. We just expand on the feature vector *x* and add more terms like *x _{0}^{2}*,

*x*,...

_{0}^{3}Note that the values are going to be wildly off once you start squaring, so we'd need feature scaling.

#### Normal equation

Thus far we've looked at minimizing *J* using gradient descent. There's a second way to calculate this without iteration.

Apparently the slow part is calculating the inverse of a matrix, which is *O(n^3)*.

#### Vectorization

When implementing learning algorithms, avoid using for-loops and try to use vector and matrix multiplications. Generally the process of refactoring to use vector is called *vectorization*.

When using Octave, I found it useful to use the vectorized application of *h(x)*:

*h _{θ}(X) = Xθ*

Instead of calculating *h _{θ}(x)* one row at a time, this calculates the whole matrix in a single shot.

### Logistic regression

#### Classification

Classification predicts discrete class. For instance, filtering email to spam/non-spam.

*y ∈ { 0, 1 }*

0 is called *negative class*, and 1 *positive class* for binary classification problem. There are also multiclass classification problem with more y's.

One might try to apply linear regression to classfication problem by saying *y = 1* when *h _{θ}(x) >= 0.5*, but apparently it's not such a good idea.

For example, let's say the *x* is size of a tumor and *y = 1* means malignant, the above shows that adding a large data point can move the decison boundary to the right.

We can setup a better suited model for classification called *logistic regression*.

By plugging *θ ^{T}x* into

*logistic function g(z)*that gives sigmoid curve, we can turn real values into 0, 1 signal. When

*z == 0*,

*g(z)*returns 0.5. Another way of thinking about this is that

*h*will now give us the probability that

_{θ}(x)*y = 1*.

#### Decision boundary

Now that we can make *h _{θ}(x)* to output sigmoid (S-shaped) curve, we can treat

*h*as

_{θ}(x) >= 0.5*y = 1*. Looking at it from

*z*in

*g(z)*, we can say that

*y = 1*when

*z >= 0*, or

*θ*.

^{T}x >= 0So to understand the *decision boundary*, we can look at where *θ ^{T}x == 0*. Here are some examples of the decision boundaries.

Note that once *θ* is picked, the decision boundary is a property of the hypothesis function (*h _{θ}(x)*).

#### Cost function of logistic regression

The cost function for the logistic regression is a bit involved. We can't use the same cost function *J(θ)* apparently because it will cause the output to be wavy (many local optima). Here's a convex alternative:

It looks gnarly at first, but remember that *y ∈ { 0, 1 }*. By multiplying *y* or *(1 - y)*, half of the term goes away, emulating the "if *y = 1*" clauses. Another thing to keep in mind is that if *h _{θ}(x)* somehow manages to compute

*h(x) = 0*when

*y = 1*, then this cost function will return a whopping penalty of

*+∞*.

The gradient descent is actually surprisingly simple:

Whatever you plug into *h _{θ}(x)* is different, but the shape of the algorithm looks identical to that of linear regression version.

#### Optimization problem

The general problem of minimizing *J(θ)* for *θ* is called the optimization problem. Gradient descent is one approach is there are faster ones like conjugate gradient, BFGS, and L-BFGS. In Octave, `fminunc`

is one of the optimizers.

#### Multiclass classification

To expand the model to deal with *y ∈ { 0, 1, ..., n }*, we take an approach calle one-vs-all where we divide the problem into *n + 1* binary classification problems, and thus *n + 1* hypotheses.

To make a prediction on a new *x*, pick the class that maximizes *h _{θ}(x)*.

- Login to post comments