现在的位置: 首页 > 综合 > 正文

Regression with Gradient Descent in Low-level Matlab

2013年08月23日 ⁄ 综合 ⁄ 共 2683字 ⁄ 字号 评论关闭

from: http://www.csrdu.org/nauman/2010/06/25/regression-with-gradient-descent-in-low-level-matlab/

 

I just finished writing my first machine learning algorithm in
Matlab. The algorithm is based on gradient descent search for estimating
parameters of linear regression (but can be easily extended to
quadratic or even higher-dimensional polynomials). It’s fairly easy if
you know the theory behind the model. So, first a very brief theory
portion. This isn’t a tutorial on statistics. Go read a book if you
don’t know about regression.

 

Just to recall: Regression comes in when you want to estimate a response variable (y
) given explanatory variables (x_i
). The goal for linear regression is to come up with values for parameters
(/theta_i
) such that you get a ‘best possible fit’ for each jth instance in the dataset of m instances:

y^j = /sum_{i=1}^m{/theta_i  /times x_i^j}

In matrix notation, that comes simply to:

Y = /theta^T X

(For ease, we assume that x_1
is a vector of all 1s thus making /theta_1
the y-intercept).

The idea of gradient descent is to come up with the best (locally) values of /theta
. It does this by repeatedly updating the value of /theta
using the formula:

/theta_i = /theta_i - /alpha * /sum_{j=1}^m{(/theta^T X^j - Y^j) X_i^j}

If you have no idea what this is or if you want to know this in-depth, read till the end.

Here’s the Matlab code for this whole procedure I wrote at first. Note the use of W for /theta
:

 

 

Then I figured that I was just doing loops and didn’t really need the
matrices. So, I went back and thought about the whole nested loop thing
and replaced the loop portion with this:

 

 

Seems suspiciously like the original formula, doesn’t it? Here’s what
the plot look like. The red line shows the linear regression line
calculated from a closed-form formula and the other lines show
estimations of /theta
with the darkest colored ones being the initial guess and lighter ones are successive guesses.

And finally, as promised, here’s where you can learn all about this stuff: machine learning video lectures from Stanford
.

抱歉!评论已关闭.