public function predict($x, $threshold = 0.5) { $prob = $this->sigmoid(parent::predict($x)); if ($threshold === null) { return $prob; } return $prob > $threshold; }
public function testPredict() { $xs = array(array(1, 2, 2, 2), array(1, 3, 3, 3), array(1, 4, 4, 4), array(1, 5, 5, 5)); $ys = array(6, 9, 12, 15); $parameters = array(0, 0, 0, 0); $gd = new LL_GradientDescent_Regression($xs, $ys, $parameters); $gd->setLearningRate(0.01); $gd->setRepetitions(10000); $gd->train(); $results = $gd->getParameters(); $estimates = array(); foreach ($xs as $index => $x_row) { $estimates[$index] = 0; foreach ($x_row as $paramIndex => $xi) { $estimates[$index] += $results[$paramIndex] * $xi; } } // manually computed estimates. foreach ($estimates as $index => $est) { $this->assertEquals($gd->predict($xs[$index]), $est); } }