PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Linear Regression in PHP (part 2)

Note: This article was originally published at Planet PHP on 19 October 2011.
Planet PHP

In the last post we had a simple stepping algorithm, and a gradient descent implementation, for fitting a line to a set of points with one variable and one 'outcome'. As I mentioned though, it's fairly straightforward to extend that to multiple variables, and even to curves, rather than just straight lines.

For this example I've reorganised the code slightly into a class to make life a little easier, but the main changes are just the hypothesis and learn functions. For the hypothesis, we just need to calculate our output value based on all of the parameters, so we turn our calculation into a loop:


/**
A* Generate a score based on the data and passed parameters
A*
A* @param array $params
A* @return int
A*/
protected function mv_hypothesis($rowdata, $params) {
A A A A $score = $params[0];
A A A A foreach($rowdata as $id = $value) A{
A A A A A A A A $score += $value * $params[$id+1];
A A A A }
A A A A return $score;
}

Similarly, we need to update our learning function. In the single parameter case we were using updating our intercept based on the score, and then we were multiplying our gradient (the second parameter) by the example row's value - so our gradient would be multiplied by the example's x score. We're going to do the same here, updating each parameter after the first with the value multiploed by the example's xn value - so parameter1 * x0, parameter2 * x1 and so on.


/**
A* Update the parameters, including using a dummy row value
A* of 1 for the first parameter.
A*
A* @param array $params
A* @return array
A*/
protected function learn($params) {
A A A A $data_rate = 1/count($this-data);

A A A A foreach($params as $id = $p) {
A A A A A A A A foreach($this-data as $row) {
A A A A A A A A A A A A $score = $this-mv_hypothesis($row[0], $params) - $row[1];
A A A A A A A A
A A A A A A A A A A A A // Update parameters
A A A A A A A A A A A A $params[$id] -= $this-learning_rate *

Truncated by Planet PHP, read more at the original (another 26775 bytes)