Run newest php-cs-fixer (#108)

This commit is contained in:
Ante Lucic 2017-07-26 02:24:47 -04:00 committed by Arkadiusz Kondas
parent 08d974bb4c
commit 07041ec608
11 changed files with 70 additions and 32 deletions

View File

@ -101,7 +101,8 @@ class DecisionTree implements Classifier
} elseif (count($this->columnNames) > $this->featureCount) { } elseif (count($this->columnNames) > $this->featureCount) {
$this->columnNames = array_slice($this->columnNames, 0, $this->featureCount); $this->columnNames = array_slice($this->columnNames, 0, $this->featureCount);
} elseif (count($this->columnNames) < $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) range(count($this->columnNames), $this->featureCount - 1)
); );
} }

View File

@ -39,9 +39,12 @@ class Adaline extends Perceptron
* *
* @throws \Exception * @throws \Exception
*/ */
public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, public function __construct(
bool $normalizeInputs = true, int $trainingType = self::BATCH_TRAINING) 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])) { 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"); throw new \Exception("Adaline can only be trained with batch and online/stochastic gradient descent algorithm");
} }

View File

@ -67,10 +67,13 @@ class LogisticRegression extends Adaline
* *
* @throws \Exception * @throws \Exception
*/ */
public function __construct(int $maxIterations = 500, bool $normalizeInputs = true, public function __construct(
int $trainingType = self::CONJUGATE_GRAD_TRAINING, string $cost = 'sse', int $maxIterations = 500,
string $penalty = 'L2') bool $normalizeInputs = true,
{ int $trainingType = self::CONJUGATE_GRAD_TRAINING,
string $cost = 'sse',
string $penalty = 'L2'
) {
$trainingTypes = range(self::BATCH_TRAINING, self::CONJUGATE_GRAD_TRAINING); $trainingTypes = range(self::BATCH_TRAINING, self::CONJUGATE_GRAD_TRAINING);
if (!in_array($trainingType, $trainingTypes)) { if (!in_array($trainingType, $trainingTypes)) {
throw new \Exception("Logistic regression can only be trained with " . throw new \Exception("Logistic regression can only be trained with " .

View File

@ -99,11 +99,11 @@ class Perceptron implements Classifier, IncrementalEstimator
$this->trainByLabel($samples, $targets, $labels); $this->trainByLabel($samples, $targets, $labels);
} }
/** /**
* @param array $samples * @param array $samples
* @param array $targets * @param array $targets
* @param array $labels * @param array $labels
*/ */
public function trainBinary(array $samples, array $targets, array $labels) public function trainBinary(array $samples, array $targets, array $labels)
{ {
if ($this->normalizer) { if ($this->normalizer) {

View File

@ -22,8 +22,14 @@ class SVC extends SupportVectorMachine implements Classifier
* @param bool $probabilityEstimates * @param bool $probabilityEstimates
*/ */
public function __construct( public function __construct(
int $kernel = Kernel::LINEAR, float $cost = 1.0, int $degree = 3, float $gamma = null, float $coef0 = 0.0, int $kernel = Kernel::LINEAR,
float $tolerance = 0.001, int $cacheSize = 100, bool $shrinking = true, 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 bool $probabilityEstimates = false
) { ) {
parent::__construct(Type::C_SVC, $kernel, $cost, 0.5, $degree, $gamma, $coef0, 0.1, $tolerance, $cacheSize, $shrinking, $probabilityEstimates); parent::__construct(Type::C_SVC, $kernel, $cost, 0.5, $degree, $gamma, $coef0, 0.1, $tolerance, $cacheSize, $shrinking, $probabilityEstimates);

View File

@ -243,7 +243,9 @@ class StochasticGD extends Optimizer
function ($w1, $w2) { function ($w1, $w2) {
return abs($w1 - $w2) > $this->threshold ? 1 : 0; return abs($w1 - $w2) > $this->threshold ? 1 : 0;
}, },
$oldTheta, $this->theta); $oldTheta,
$this->theta
);
if (array_sum($diff) == 0) { if (array_sum($diff) == 0) {
return true; return true;

View File

@ -301,7 +301,7 @@ class EigenvalueDecomposition
$p = -$s * $s2 * $c3 * $el1 * $this->e[$l] / $dl1; $p = -$s * $s2 * $c3 * $el1 * $this->e[$l] / $dl1;
$this->e[$l] = $s * $p; $this->e[$l] = $s * $p;
$this->d[$l] = $c * $p; $this->d[$l] = $c * $p;
// Check for convergence. // Check for convergence.
} while (abs($this->e[$l]) > $eps * $tst1); } while (abs($this->e[$l]) > $eps * $tst1);
} }
$this->d[$l] = $this->d[$l] + $f; $this->d[$l] = $this->d[$l] + $f;
@ -493,7 +493,7 @@ class EigenvalueDecomposition
$this->e[$n] = 0.0; $this->e[$n] = 0.0;
--$n; --$n;
$iter = 0; $iter = 0;
// Two roots found // Two roots found
} elseif ($l == $n - 1) { } elseif ($l == $n - 1) {
$w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n];
$p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0; $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 - 1] = $q * $z + $p * $this->V[$i][$n];
$this->V[$i][$n] = $q * $this->V[$i][$n] - $p * $z; $this->V[$i][$n] = $q * $this->V[$i][$n] - $p * $z;
} }
// Complex pair // Complex pair
} else { } else {
$this->d[$n - 1] = $x + $p; $this->d[$n - 1] = $x + $p;
$this->d[$n] = $x + $p; $this->d[$n] = $x + $p;
@ -550,7 +550,7 @@ class EigenvalueDecomposition
} }
$n = $n - 2; $n = $n - 2;
$iter = 0; $iter = 0;
// No convergence yet // No convergence yet
} else { } else {
// Form shift // Form shift
$x = $this->H[$n][$n]; $x = $this->H[$n][$n];
@ -715,7 +715,7 @@ class EigenvalueDecomposition
} else { } else {
$this->H[$i][$n] = -$r / ($eps * $norm); $this->H[$i][$n] = -$r / ($eps * $norm);
} }
// Solve real equations // Solve real equations
} else { } else {
$x = $this->H[$i][$i + 1]; $x = $this->H[$i][$i + 1];
$y = $this->H[$i + 1][$i]; $y = $this->H[$i + 1][$i];
@ -737,7 +737,7 @@ class EigenvalueDecomposition
} }
} }
} }
// Complex vector // Complex vector
} elseif ($q < 0) { } elseif ($q < 0) {
$l = $n - 1; $l = $n - 1;
// Last vector component imaginary so matrix is triangular // Last vector component imaginary so matrix is triangular

View File

@ -150,7 +150,12 @@ class Covariance
$cov[$i][$k] = $cov[$k][$i]; $cov[$i][$k] = $cov[$k][$i];
} else { } else {
$cov[$i][$k] = self::fromDataset( $cov[$i][$k] = self::fromDataset(
$data, $i, $k, true, $means[$i], $means[$k] $data,
$i,
$k,
true,
$means[$i],
$means[$k]
); );
} }
} }

View File

@ -22,9 +22,15 @@ class SVR extends SupportVectorMachine implements Regression
* @param bool $shrinking * @param bool $shrinking
*/ */
public function __construct( public function __construct(
int $kernel = Kernel::RBF, int $degree = 3, float $epsilon = 0.1, float $cost = 1.0, int $kernel = Kernel::RBF,
float $gamma = null, float $coef0 = 0.0, float $tolerance = 0.001, int $degree = 3,
int $cacheSize = 100, bool $shrinking = true 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); parent::__construct(Type::EPSILON_SVR, $kernel, $cost, 0.5, $degree, $gamma, $coef0, $epsilon, $tolerance, $cacheSize, $shrinking, false);
} }

View File

@ -105,9 +105,18 @@ class SupportVectorMachine
* @param bool $probabilityEstimates * @param bool $probabilityEstimates
*/ */
public function __construct( public function __construct(
int $type, int $kernel, float $cost = 1.0, float $nu = 0.5, int $degree = 3, int $type,
float $gamma = null, float $coef0 = 0.0, float $epsilon = 0.1, float $tolerance = 0.001, int $kernel,
int $cacheSize = 100, bool $shrinking = true, bool $probabilityEstimates = false 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->type = $type;
$this->kernel = $kernel; $this->kernel = $kernel;
@ -241,7 +250,8 @@ class SupportVectorMachine
*/ */
private function buildTrainCommand(string $trainingSetFileName, string $modelFileName): string 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->binPath,
$this->getOSExtension(), $this->getOSExtension(),
$this->type, $this->type,

View File

@ -124,10 +124,12 @@ class NormalizerTest extends TestCase
// Values in the vector should be some value between -3 and +3 // Values in the vector should be some value between -3 and +3
$this->assertCount(10, $samples); $this->assertCount(10, $samples);
foreach ($samples as $sample) { foreach ($samples as $sample) {
$errors = array_filter($sample, $errors = array_filter(
$sample,
function ($element) { function ($element) {
return $element < -3 || $element > 3; return $element < -3 || $element > 3;
}); }
);
$this->assertCount(0, $errors); $this->assertCount(0, $errors);
$this->assertEquals(0, $sample[3]); $this->assertEquals(0, $sample[3]);
} }