@@ -4,7 +4,6 @@ |
||
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Classification\Linear; |
| 6 | 6 | |
| 7 | -use Phpml\Classification\Classifier; |
|
| 8 | 7 | use Phpml\Helper\Optimizer\ConjugateGradient; |
| 9 | 8 | |
| 10 | 9 | class LogisticRegression extends Adaline |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Classification\Linear; |
| 6 | 6 | |
@@ -13,12 +13,12 @@ discard block |
||
| 13 | 13 | /** |
| 14 | 14 | * Batch training: Gradient descent algorithm (default) |
| 15 | 15 | */ |
| 16 | - const BATCH_TRAINING = 1; |
|
| 16 | + const BATCH_TRAINING = 1; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Online training: Stochastic gradient descent learning |
| 20 | 20 | */ |
| 21 | - const ONLINE_TRAINING = 2; |
|
| 21 | + const ONLINE_TRAINING = 2; |
|
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Conjugate Batch: Conjugate Gradient algorithm |
@@ -74,14 +74,14 @@ discard block |
||
| 74 | 74 | string $penalty = 'L2') |
| 75 | 75 | { |
| 76 | 76 | $trainingTypes = range(self::BATCH_TRAINING, self::CONJUGATE_GRAD_TRAINING); |
| 77 | - if (! in_array($trainingType, $trainingTypes)) { |
|
| 78 | - throw new \Exception("Logistic regression can only be trained with " . |
|
| 79 | - "batch (gradient descent), online (stochastic gradient descent) " . |
|
| 77 | + if (!in_array($trainingType, $trainingTypes)) { |
|
| 78 | + throw new \Exception("Logistic regression can only be trained with ". |
|
| 79 | + "batch (gradient descent), online (stochastic gradient descent) ". |
|
| 80 | 80 | "or conjugate batch (conjugate gradients) algorithms"); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - if (! in_array($cost, ['log', 'sse'])) { |
|
| 84 | - throw new \Exception("Logistic regression cost function can be one of the following: \n" . |
|
| 83 | + if (!in_array($cost, ['log', 'sse'])) { |
|
| 84 | + throw new \Exception("Logistic regression cost function can be one of the following: \n". |
|
| 85 | 85 | "'log' for log-likelihood and 'sse' for sum of squared errors"); |
| 86 | 86 | } |
| 87 | 87 | |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | * The gradient of the cost function to be used with gradient descent: |
| 178 | 178 | * ∇J(x) = -(y - h(x)) = (h(x) - y) |
| 179 | 179 | */ |
| 180 | - $callback = function ($weights, $sample, $y) use ($penalty) { |
|
| 180 | + $callback = function($weights, $sample, $y) use ($penalty) { |
|
| 181 | 181 | $this->weights = $weights; |
| 182 | 182 | $hX = $this->output($sample); |
| 183 | 183 | |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | * The gradient of the cost function: |
| 209 | 209 | * ∇J(x) = -(h(x) - y) . h(x) . (1 - h(x)) |
| 210 | 210 | */ |
| 211 | - $callback = function ($weights, $sample, $y) use ($penalty) { |
|
| 211 | + $callback = function($weights, $sample, $y) use ($penalty) { |
|
| 212 | 212 | $this->weights = $weights; |
| 213 | 213 | $hX = $this->output($sample); |
| 214 | 214 | |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | { |
| 16 | 16 | use Predictable, OneVsRest; |
| 17 | 17 | |
| 18 | - /** |
|
| 18 | + /** |
|
| 19 | 19 | * @var array |
| 20 | 20 | */ |
| 21 | 21 | protected $samples = []; |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | $this->maxIterations = $maxIterations; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - /** |
|
| 86 | + /** |
|
| 87 | 87 | * @param array $samples |
| 88 | 88 | * @param array $targets |
| 89 | 89 | */ |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Classification\Linear; |
| 6 | 6 | |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | protected function runTraining() |
| 119 | 119 | { |
| 120 | 120 | // The cost function is the sum of squares |
| 121 | - $callback = function ($weights, $sample, $target) { |
|
| 121 | + $callback = function($weights, $sample, $target) { |
|
| 122 | 122 | $this->weights = $weights; |
| 123 | 123 | |
| 124 | 124 | $prediction = $this->outputClass($sample); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | protected function runGradientDescent(\Closure $gradientFunc, bool $isBatch = false) |
| 139 | 139 | { |
| 140 | - $class = $isBatch ? GD::class : StochasticGD::class; |
|
| 140 | + $class = $isBatch ? GD::class : StochasticGD::class; |
|
| 141 | 141 | |
| 142 | 142 | $optimizer = (new $class($this->featureCount)) |
| 143 | 143 | ->setLearningRate($this->learningRate) |
@@ -227,6 +227,6 @@ discard block |
||
| 227 | 227 | |
| 228 | 228 | $predictedClass = $this->outputClass($sample); |
| 229 | 229 | |
| 230 | - return $this->labels[ $predictedClass ]; |
|
| 230 | + return $this->labels[$predictedClass]; |
|
| 231 | 231 | } |
| 232 | 232 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Classification\Linear; |
| 6 | 6 | |
@@ -12,12 +12,12 @@ discard block |
||
| 12 | 12 | /** |
| 13 | 13 | * Batch training is the default Adaline training algorithm |
| 14 | 14 | */ |
| 15 | - const BATCH_TRAINING = 1; |
|
| 15 | + const BATCH_TRAINING = 1; |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Online training: Stochastic gradient descent learning |
| 19 | 19 | */ |
| 20 | - const ONLINE_TRAINING = 2; |
|
| 20 | + const ONLINE_TRAINING = 2; |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Training type may be either 'Batch' or 'Online' learning |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, |
| 42 | 42 | bool $normalizeInputs = true, int $trainingType = self::BATCH_TRAINING) |
| 43 | 43 | { |
| 44 | - if (! in_array($trainingType, [self::BATCH_TRAINING, self::ONLINE_TRAINING])) { |
|
| 44 | + if (!in_array($trainingType, [self::BATCH_TRAINING, self::ONLINE_TRAINING])) { |
|
| 45 | 45 | throw new \Exception("Adaline can only be trained with batch and online/stochastic gradient descent algorithm"); |
| 46 | 46 | } |
| 47 | 47 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | protected function runTraining() |
| 58 | 58 | { |
| 59 | 59 | // The cost function is the sum of squares |
| 60 | - $callback = function ($weights, $sample, $target) { |
|
| 60 | + $callback = function($weights, $sample, $target) { |
|
| 61 | 61 | $this->weights = $weights; |
| 62 | 62 | |
| 63 | 63 | $output = $this->output($sample); |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Helper\Optimizer; |
| 6 | 6 | |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | $this->updateWeightsWithUpdates($updates, $totalPenalty); |
| 44 | 44 | |
| 45 | - $this->costValues[] = array_sum($errors)/$this->sampleCount; |
|
| 45 | + $this->costValues[] = array_sum($errors) / $this->sampleCount; |
|
| 46 | 46 | |
| 47 | 47 | if ($this->earlyStop($theta)) { |
| 48 | 48 | break; |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | protected function gradient(array $theta) |
| 64 | 64 | { |
| 65 | 65 | $costs = []; |
| 66 | - $gradient= []; |
|
| 66 | + $gradient = []; |
|
| 67 | 67 | $totalPenalty = 0; |
| 68 | 68 | |
| 69 | 69 | foreach ($this->samples as $index => $sample) { |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | list($cost, $grad, $penalty) = array_pad($result, 3, 0); |
| 74 | 74 | |
| 75 | 75 | $costs[] = $cost; |
| 76 | - $gradient[]= $grad; |
|
| 76 | + $gradient[] = $grad; |
|
| 77 | 77 | $totalPenalty += $penalty; |
| 78 | 78 | } |
| 79 | 79 | |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | protected function updateWeightsWithUpdates(array $updates, float $penalty) |
| 90 | 90 | { |
| 91 | 91 | // Updates all weights at once |
| 92 | - for ($i=0; $i <= $this->dimensions; $i++) { |
|
| 92 | + for ($i = 0; $i <= $this->dimensions; $i++) { |
|
| 93 | 93 | if ($i == 0) { |
| 94 | 94 | $this->theta[0] -= $this->learningRate * array_sum($updates); |
| 95 | 95 | } else { |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Helper\Optimizer; |
| 6 | 6 | |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @var array |
| 74 | 74 | */ |
| 75 | - protected $costValues= []; |
|
| 75 | + protected $costValues = []; |
|
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * Initializes the SGD optimizer for the given number of dimensions |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | $this->theta[0] -= $this->learningRate * $gradient; |
| 217 | 217 | |
| 218 | 218 | // Update other values |
| 219 | - for ($i=1; $i <= $this->dimensions; $i++) { |
|
| 219 | + for ($i = 1; $i <= $this->dimensions; $i++) { |
|
| 220 | 220 | $this->theta[$i] -= $this->learningRate * |
| 221 | 221 | ($gradient * $sample[$i - 1] + $penalty * $this->theta[$i]); |
| 222 | 222 | } |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | { |
| 241 | 241 | // Check for early stop: No change larger than threshold (default 1e-5) |
| 242 | 242 | $diff = array_map( |
| 243 | - function ($w1, $w2) { |
|
| 243 | + function($w1, $w2) { |
|
| 244 | 244 | return abs($w1 - $w2) > $this->threshold ? 1 : 0; |
| 245 | 245 | }, |
| 246 | 246 | $oldTheta, $this->theta); |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Helper\Optimizer; |
| 6 | 6 | |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | // Inits the weights randomly |
| 33 | 33 | $this->theta = []; |
| 34 | - for ($i=0; $i < $this->dimensions; $i++) { |
|
| 34 | + for ($i = 0; $i < $this->dimensions; $i++) { |
|
| 35 | 35 | $this->theta[] = rand() / (float) getrandmax(); |
| 36 | 36 | } |
| 37 | 37 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Helper\Optimizer; |
| 6 | 6 | |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | $d = mp::muls($this->gradient($this->theta), -1); |
| 36 | 36 | |
| 37 | - for ($i=0; $i < $this->maxIterations; $i++) { |
|
| 37 | + for ($i = 0; $i < $this->maxIterations; $i++) { |
|
| 38 | 38 | // Obtain α that minimizes f(θ + α.d) |
| 39 | 39 | $alpha = $this->getAlpha(array_sum($d)); |
| 40 | 40 | |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | { |
| 162 | 162 | $theta = $this->theta; |
| 163 | 163 | |
| 164 | - for ($i=0; $i < $this->dimensions + 1; $i++) { |
|
| 164 | + for ($i = 0; $i < $this->dimensions + 1; $i++) { |
|
| 165 | 165 | if ($i == 0) { |
| 166 | 166 | $theta[$i] += $alpha * array_sum($d); |
| 167 | 167 | } else { |