From 07041ec60809b80ec871ab344502615293e9e2e9 Mon Sep 17 00:00:00 2001 From: Ante Lucic Date: Wed, 26 Jul 2017 02:24:47 -0400 Subject: [PATCH] Run newest php-cs-fixer (#108) --- src/Phpml/Classification/DecisionTree.php | 3 ++- src/Phpml/Classification/Linear/Adaline.php | 9 ++++++--- .../Linear/LogisticRegression.php | 11 +++++++---- src/Phpml/Classification/Linear/Perceptron.php | 10 +++++----- src/Phpml/Classification/SVC.php | 10 ++++++++-- src/Phpml/Helper/Optimizer/StochasticGD.php | 4 +++- .../LinearAlgebra/EigenvalueDecomposition.php | 12 ++++++------ src/Phpml/Math/Statistic/Covariance.php | 7 ++++++- src/Phpml/Regression/SVR.php | 12 +++++++++--- .../SupportVectorMachine.php | 18 ++++++++++++++---- tests/Phpml/Preprocessing/NormalizerTest.php | 6 ++++-- 11 files changed, 70 insertions(+), 32 deletions(-) diff --git a/src/Phpml/Classification/DecisionTree.php b/src/Phpml/Classification/DecisionTree.php index da8b81b..c0c71f3 100644 --- a/src/Phpml/Classification/DecisionTree.php +++ b/src/Phpml/Classification/DecisionTree.php @@ -101,7 +101,8 @@ class DecisionTree implements Classifier } elseif (count($this->columnNames) > $this->featureCount) { $this->columnNames = array_slice($this->columnNames, 0, $this->featureCount); } elseif (count($this->columnNames) < $this->featureCount) { - $this->columnNames = array_merge($this->columnNames, + $this->columnNames = array_merge( + $this->columnNames, range(count($this->columnNames), $this->featureCount - 1) ); } diff --git a/src/Phpml/Classification/Linear/Adaline.php b/src/Phpml/Classification/Linear/Adaline.php index b94de28..df648e8 100644 --- a/src/Phpml/Classification/Linear/Adaline.php +++ b/src/Phpml/Classification/Linear/Adaline.php @@ -39,9 +39,12 @@ class Adaline extends Perceptron * * @throws \Exception */ - public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, - bool $normalizeInputs = true, int $trainingType = self::BATCH_TRAINING) - { + public function __construct( + float $learningRate = 0.001, + int $maxIterations = 1000, + bool $normalizeInputs = true, + int $trainingType = self::BATCH_TRAINING + ) { if (!in_array($trainingType, [self::BATCH_TRAINING, self::ONLINE_TRAINING])) { throw new \Exception("Adaline can only be trained with batch and online/stochastic gradient descent algorithm"); } diff --git a/src/Phpml/Classification/Linear/LogisticRegression.php b/src/Phpml/Classification/Linear/LogisticRegression.php index bc6a3c9..90ef4d1 100644 --- a/src/Phpml/Classification/Linear/LogisticRegression.php +++ b/src/Phpml/Classification/Linear/LogisticRegression.php @@ -67,10 +67,13 @@ class LogisticRegression extends Adaline * * @throws \Exception */ - public function __construct(int $maxIterations = 500, bool $normalizeInputs = true, - int $trainingType = self::CONJUGATE_GRAD_TRAINING, string $cost = 'sse', - string $penalty = 'L2') - { + public function __construct( + int $maxIterations = 500, + bool $normalizeInputs = true, + int $trainingType = self::CONJUGATE_GRAD_TRAINING, + string $cost = 'sse', + string $penalty = 'L2' + ) { $trainingTypes = range(self::BATCH_TRAINING, self::CONJUGATE_GRAD_TRAINING); if (!in_array($trainingType, $trainingTypes)) { throw new \Exception("Logistic regression can only be trained with " . diff --git a/src/Phpml/Classification/Linear/Perceptron.php b/src/Phpml/Classification/Linear/Perceptron.php index f4a8791..145a992 100644 --- a/src/Phpml/Classification/Linear/Perceptron.php +++ b/src/Phpml/Classification/Linear/Perceptron.php @@ -99,11 +99,11 @@ class Perceptron implements Classifier, IncrementalEstimator $this->trainByLabel($samples, $targets, $labels); } - /** - * @param array $samples - * @param array $targets - * @param array $labels - */ + /** + * @param array $samples + * @param array $targets + * @param array $labels + */ public function trainBinary(array $samples, array $targets, array $labels) { if ($this->normalizer) { diff --git a/src/Phpml/Classification/SVC.php b/src/Phpml/Classification/SVC.php index 38ae9c4..bba5d09 100644 --- a/src/Phpml/Classification/SVC.php +++ b/src/Phpml/Classification/SVC.php @@ -22,8 +22,14 @@ class SVC extends SupportVectorMachine implements Classifier * @param bool $probabilityEstimates */ public function __construct( - int $kernel = Kernel::LINEAR, float $cost = 1.0, int $degree = 3, float $gamma = null, float $coef0 = 0.0, - float $tolerance = 0.001, int $cacheSize = 100, bool $shrinking = true, + int $kernel = Kernel::LINEAR, + float $cost = 1.0, + int $degree = 3, + float $gamma = null, + float $coef0 = 0.0, + float $tolerance = 0.001, + int $cacheSize = 100, + bool $shrinking = true, bool $probabilityEstimates = false ) { parent::__construct(Type::C_SVC, $kernel, $cost, 0.5, $degree, $gamma, $coef0, 0.1, $tolerance, $cacheSize, $shrinking, $probabilityEstimates); diff --git a/src/Phpml/Helper/Optimizer/StochasticGD.php b/src/Phpml/Helper/Optimizer/StochasticGD.php index fa2401a..82e860a 100644 --- a/src/Phpml/Helper/Optimizer/StochasticGD.php +++ b/src/Phpml/Helper/Optimizer/StochasticGD.php @@ -243,7 +243,9 @@ class StochasticGD extends Optimizer function ($w1, $w2) { return abs($w1 - $w2) > $this->threshold ? 1 : 0; }, - $oldTheta, $this->theta); + $oldTheta, + $this->theta + ); if (array_sum($diff) == 0) { return true; diff --git a/src/Phpml/Math/LinearAlgebra/EigenvalueDecomposition.php b/src/Phpml/Math/LinearAlgebra/EigenvalueDecomposition.php index 7f0ec4b..642e8b3 100644 --- a/src/Phpml/Math/LinearAlgebra/EigenvalueDecomposition.php +++ b/src/Phpml/Math/LinearAlgebra/EigenvalueDecomposition.php @@ -301,7 +301,7 @@ class EigenvalueDecomposition $p = -$s * $s2 * $c3 * $el1 * $this->e[$l] / $dl1; $this->e[$l] = $s * $p; $this->d[$l] = $c * $p; - // Check for convergence. + // Check for convergence. } while (abs($this->e[$l]) > $eps * $tst1); } $this->d[$l] = $this->d[$l] + $f; @@ -493,7 +493,7 @@ class EigenvalueDecomposition $this->e[$n] = 0.0; --$n; $iter = 0; - // Two roots found + // Two roots found } elseif ($l == $n - 1) { $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; $p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0; @@ -541,7 +541,7 @@ class EigenvalueDecomposition $this->V[$i][$n - 1] = $q * $z + $p * $this->V[$i][$n]; $this->V[$i][$n] = $q * $this->V[$i][$n] - $p * $z; } - // Complex pair + // Complex pair } else { $this->d[$n - 1] = $x + $p; $this->d[$n] = $x + $p; @@ -550,7 +550,7 @@ class EigenvalueDecomposition } $n = $n - 2; $iter = 0; - // No convergence yet + // No convergence yet } else { // Form shift $x = $this->H[$n][$n]; @@ -715,7 +715,7 @@ class EigenvalueDecomposition } else { $this->H[$i][$n] = -$r / ($eps * $norm); } - // Solve real equations + // Solve real equations } else { $x = $this->H[$i][$i + 1]; $y = $this->H[$i + 1][$i]; @@ -737,7 +737,7 @@ class EigenvalueDecomposition } } } - // Complex vector + // Complex vector } elseif ($q < 0) { $l = $n - 1; // Last vector component imaginary so matrix is triangular diff --git a/src/Phpml/Math/Statistic/Covariance.php b/src/Phpml/Math/Statistic/Covariance.php index 8c8781d..779b895 100644 --- a/src/Phpml/Math/Statistic/Covariance.php +++ b/src/Phpml/Math/Statistic/Covariance.php @@ -150,7 +150,12 @@ class Covariance $cov[$i][$k] = $cov[$k][$i]; } else { $cov[$i][$k] = self::fromDataset( - $data, $i, $k, true, $means[$i], $means[$k] + $data, + $i, + $k, + true, + $means[$i], + $means[$k] ); } } diff --git a/src/Phpml/Regression/SVR.php b/src/Phpml/Regression/SVR.php index e32eeb0..d4e5651 100644 --- a/src/Phpml/Regression/SVR.php +++ b/src/Phpml/Regression/SVR.php @@ -22,9 +22,15 @@ class SVR extends SupportVectorMachine implements Regression * @param bool $shrinking */ public function __construct( - int $kernel = Kernel::RBF, int $degree = 3, float $epsilon = 0.1, float $cost = 1.0, - float $gamma = null, float $coef0 = 0.0, float $tolerance = 0.001, - int $cacheSize = 100, bool $shrinking = true + int $kernel = Kernel::RBF, + int $degree = 3, + float $epsilon = 0.1, + float $cost = 1.0, + float $gamma = null, + float $coef0 = 0.0, + float $tolerance = 0.001, + int $cacheSize = 100, + bool $shrinking = true ) { parent::__construct(Type::EPSILON_SVR, $kernel, $cost, 0.5, $degree, $gamma, $coef0, $epsilon, $tolerance, $cacheSize, $shrinking, false); } diff --git a/src/Phpml/SupportVectorMachine/SupportVectorMachine.php b/src/Phpml/SupportVectorMachine/SupportVectorMachine.php index c6ec017..b29bfa5 100644 --- a/src/Phpml/SupportVectorMachine/SupportVectorMachine.php +++ b/src/Phpml/SupportVectorMachine/SupportVectorMachine.php @@ -105,9 +105,18 @@ class SupportVectorMachine * @param bool $probabilityEstimates */ public function __construct( - int $type, int $kernel, float $cost = 1.0, float $nu = 0.5, int $degree = 3, - float $gamma = null, float $coef0 = 0.0, float $epsilon = 0.1, float $tolerance = 0.001, - int $cacheSize = 100, bool $shrinking = true, bool $probabilityEstimates = false + int $type, + int $kernel, + float $cost = 1.0, + float $nu = 0.5, + int $degree = 3, + float $gamma = null, + float $coef0 = 0.0, + float $epsilon = 0.1, + float $tolerance = 0.001, + int $cacheSize = 100, + bool $shrinking = true, + bool $probabilityEstimates = false ) { $this->type = $type; $this->kernel = $kernel; @@ -241,7 +250,8 @@ class SupportVectorMachine */ private function buildTrainCommand(string $trainingSetFileName, string $modelFileName): string { - return sprintf('%ssvm-train%s -s %s -t %s -c %s -n %s -d %s%s -r %s -p %s -m %s -e %s -h %d -b %d %s %s', + return sprintf( + '%ssvm-train%s -s %s -t %s -c %s -n %s -d %s%s -r %s -p %s -m %s -e %s -h %d -b %d %s %s', $this->binPath, $this->getOSExtension(), $this->type, diff --git a/tests/Phpml/Preprocessing/NormalizerTest.php b/tests/Phpml/Preprocessing/NormalizerTest.php index 2492fae..a8a8826 100644 --- a/tests/Phpml/Preprocessing/NormalizerTest.php +++ b/tests/Phpml/Preprocessing/NormalizerTest.php @@ -124,10 +124,12 @@ class NormalizerTest extends TestCase // Values in the vector should be some value between -3 and +3 $this->assertCount(10, $samples); foreach ($samples as $sample) { - $errors = array_filter($sample, + $errors = array_filter( + $sample, function ($element) { return $element < -3 || $element > 3; - }); + } + ); $this->assertCount(0, $errors); $this->assertEquals(0, $sample[3]); }