@@ -136,9 +136,9 @@ |
||
136 | 136 | $N_K_N = $N->multiply($K_N); |
137 | 137 | |
138 | 138 | return $K->subtract($N_K) |
139 | - ->subtract($K_N) |
|
140 | - ->add($N_K_N) |
|
141 | - ->toArray(); |
|
139 | + ->subtract($K_N) |
|
140 | + ->add($N_K_N) |
|
141 | + ->toArray(); |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | /** |
@@ -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\DimensionReduction; |
6 | 6 | |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | protected function centerMatrix(array $matrix, int $n) |
135 | 135 | { |
136 | - $N = array_fill(0, $n, array_fill(0, $n, 1.0/$n)); |
|
136 | + $N = array_fill(0, $n, array_fill(0, $n, 1.0 / $n)); |
|
137 | 137 | $N = new Matrix($N, false); |
138 | 138 | $K = new Matrix($matrix, false); |
139 | 139 | |
@@ -162,19 +162,19 @@ discard block |
||
162 | 162 | switch ($this->kernel) { |
163 | 163 | case self::KERNEL_LINEAR: |
164 | 164 | // k(x,y) = xT.y |
165 | - return function ($x, $y) { |
|
165 | + return function($x, $y) { |
|
166 | 166 | return Matrix::dot($x, $y)[0]; |
167 | 167 | }; |
168 | 168 | case self::KERNEL_RBF: |
169 | 169 | // k(x,y)=exp(-γ.|x-y|) where |..| is Euclidean distance |
170 | 170 | $dist = new Euclidean(); |
171 | - return function ($x, $y) use ($dist) { |
|
171 | + return function($x, $y) use ($dist) { |
|
172 | 172 | return exp(-$this->gamma * $dist->sqDistance($x, $y)); |
173 | 173 | }; |
174 | 174 | |
175 | 175 | case self::KERNEL_SIGMOID: |
176 | 176 | // k(x,y)=tanh(γ.xT.y+c0) where c0=1 |
177 | - return function ($x, $y) { |
|
177 | + return function($x, $y) { |
|
178 | 178 | $res = Matrix::dot($x, $y)[0] + 1.0; |
179 | 179 | return tanh($this->gamma * $res); |
180 | 180 | }; |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | case self::KERNEL_LAPLACIAN: |
183 | 183 | // k(x,y)=exp(-γ.|x-y|) where |..| is Manhattan distance |
184 | 184 | $dist = new Manhattan(); |
185 | - return function ($x, $y) use ($dist) { |
|
185 | + return function($x, $y) use ($dist) { |
|
186 | 186 | return exp(-$this->gamma * $dist->distance($x, $y)); |
187 | 187 | }; |
188 | 188 | |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | protected function projectSample(array $pairs) |
217 | 217 | { |
218 | 218 | // Normalize eigenvectors by eig = eigVectors / eigValues |
219 | - $func = function ($eigVal, $eigVect) { |
|
219 | + $func = function($eigVal, $eigVect) { |
|
220 | 220 | $m = new Matrix($eigVect, false); |
221 | 221 | $a = $m->divideByScalar($eigVal)->toArray(); |
222 | 222 |
@@ -58,21 +58,21 @@ |
||
58 | 58 | private $V = []; |
59 | 59 | |
60 | 60 | /** |
61 | - * Array for internal storage of nonsymmetric Hessenberg form. |
|
62 | - * @var array |
|
63 | - */ |
|
61 | + * Array for internal storage of nonsymmetric Hessenberg form. |
|
62 | + * @var array |
|
63 | + */ |
|
64 | 64 | private $H = []; |
65 | 65 | |
66 | 66 | /** |
67 | - * Working storage for nonsymmetric algorithm. |
|
68 | - * @var array |
|
69 | - */ |
|
67 | + * Working storage for nonsymmetric algorithm. |
|
68 | + * @var array |
|
69 | + */ |
|
70 | 70 | private $ort; |
71 | 71 | |
72 | 72 | /** |
73 | - * Used for complex scalar division. |
|
74 | - * @var float |
|
75 | - */ |
|
73 | + * Used for complex scalar division. |
|
74 | + * @var float |
|
75 | + */ |
|
76 | 76 | private $cdivr; |
77 | 77 | private $cdivi; |
78 | 78 |
@@ -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 | * |
6 | 6 | * Class to obtain eigenvalues and eigenvectors of a real matrix. |
@@ -838,7 +838,7 @@ discard block |
||
838 | 838 | |
839 | 839 | // Always return the eigenvectors of length 1.0 |
840 | 840 | $vectors = new Matrix($vectors); |
841 | - $vectors = array_map(function ($vect) { |
|
841 | + $vectors = array_map(function($vect) { |
|
842 | 842 | $sum = 0; |
843 | 843 | for ($i = 0; $i < count($vect); ++$i) { |
844 | 844 | $sum += $vect[$i] ** 2; |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | /** |
5 | 5 | * @package JAMA |
6 | 6 | * |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace Phpml\Math\Statistic; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace Phpml\DimensionReduction; |
6 | 6 |
@@ -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\DimensionReduction; |
6 | 6 | |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | protected function calculateMeans(array $data, array $classes) : array |
119 | 119 | { |
120 | 120 | $means = []; |
121 | - $counts= []; |
|
121 | + $counts = []; |
|
122 | 122 | $overallMean = array_fill(0, count($data[0]), 0.0); |
123 | 123 | |
124 | 124 | foreach ($data as $index => $row) { |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | |
148 | 148 | // Calculate overall mean of the dataset for each column |
149 | 149 | $numElements = array_sum($counts); |
150 | - $map = function ($el) use ($numElements) { |
|
150 | + $map = function($el) use ($numElements) { |
|
151 | 151 | return $el / $numElements; |
152 | 152 | }; |
153 | 153 | $this->overallMean = array_map($map, $overallMean); |
@@ -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\DimensionReduction; |
6 | 6 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | { |
55 | 55 | $eig = new EigenvalueDecomposition($matrix); |
56 | 56 | $eigVals = $eig->getRealEigenvalues(); |
57 | - $eigVects= $eig->getEigenvectors(); |
|
57 | + $eigVects = $eig->getEigenvectors(); |
|
58 | 58 | |
59 | 59 | $totalEigVal = array_sum($eigVals); |
60 | 60 | // Sort eigenvalues in descending order |
@@ -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; |
6 | 6 | |
@@ -13,8 +13,8 @@ discard block |
||
13 | 13 | { |
14 | 14 | use Trainable, Predictable; |
15 | 15 | |
16 | - const CONTINUOS = 1; |
|
17 | - const NOMINAL = 2; |
|
16 | + const CONTINUOS = 1; |
|
17 | + const NOMINAL = 2; |
|
18 | 18 | const EPSILON = 1e-10; |
19 | 19 | |
20 | 20 | /** |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | /** |
26 | 26 | * @var array |
27 | 27 | */ |
28 | - private $mean= []; |
|
28 | + private $mean = []; |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * @var array |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | private function calculateStatistics($label, $samples) |
87 | 87 | { |
88 | 88 | $this->std[$label] = array_fill(0, $this->featureCount, 0); |
89 | - $this->mean[$label]= array_fill(0, $this->featureCount, 0); |
|
89 | + $this->mean[$label] = array_fill(0, $this->featureCount, 0); |
|
90 | 90 | $this->dataType[$label] = array_fill(0, $this->featureCount, self::CONTINUOS); |
91 | 91 | $this->discreteProb[$label] = array_fill(0, $this->featureCount, self::CONTINUOS); |
92 | 92 | for ($i = 0; $i < $this->featureCount; ++$i) { |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | $this->dataType[$label][$i] = self::NOMINAL; |
101 | 101 | $this->discreteProb[$label][$i] = array_count_values($values); |
102 | 102 | $db = &$this->discreteProb[$label][$i]; |
103 | - $db = array_map(function ($el) use ($numValues) { |
|
103 | + $db = array_map(function($el) use ($numValues) { |
|
104 | 104 | return $el / $numValues; |
105 | 105 | }, $db); |
106 | 106 | } else { |
@@ -130,8 +130,8 @@ discard block |
||
130 | 130 | } |
131 | 131 | return $this->discreteProb[$label][$feature][$value]; |
132 | 132 | } |
133 | - $std = $this->std[$label][$feature] ; |
|
134 | - $mean= $this->mean[$label][$feature]; |
|
133 | + $std = $this->std[$label][$feature]; |
|
134 | + $mean = $this->mean[$label][$feature]; |
|
135 | 135 | // Calculate the probability density by use of normal/Gaussian distribution |
136 | 136 | // Ref: https://en.wikipedia.org/wiki/Normal_distribution |
137 | 137 | // |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | // some libraries adopt taking log of calculations such as |
140 | 140 | // scikit-learn did. |
141 | 141 | // (See : https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/naive_bayes.py) |
142 | - $pdf = -0.5 * log(2.0 * pi() * $std * $std); |
|
142 | + $pdf = -0.5 * log(2.0 * pi() * $std * $std); |
|
143 | 143 | $pdf -= 0.5 * pow($value - $mean, 2) / ($std * $std); |
144 | 144 | return $pdf; |
145 | 145 | } |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | $predictions = []; |
175 | 175 | foreach ($this->labels as $label) { |
176 | 176 | $p = $this->p[$label]; |
177 | - for ($i = 0; $i<$this->featureCount; ++$i) { |
|
177 | + for ($i = 0; $i < $this->featureCount; ++$i) { |
|
178 | 178 | $Plf = $this->sampleProbability($sample, $i, $label); |
179 | 179 | $p += $Plf; |
180 | 180 | } |
@@ -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 | |
@@ -192,8 +192,8 @@ discard block |
||
192 | 192 | } |
193 | 193 | |
194 | 194 | // Try other possible points one by one |
195 | - for ($step = $minValue; $step <= $maxValue; $step+= $stepSize) { |
|
196 | - $threshold = (float)$step; |
|
195 | + for ($step = $minValue; $step <= $maxValue; $step += $stepSize) { |
|
196 | + $threshold = (float) $step; |
|
197 | 197 | list($errorRate, $prob) = $this->calculateErrorRate($targets, $threshold, $operator, $values); |
198 | 198 | if ($errorRate < $split['trainingErrorRate']) { |
199 | 199 | $split = ['value' => $threshold, 'operator' => $operator, |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | { |
218 | 218 | $values = array_column($samples, $col); |
219 | 219 | $valueCounts = array_count_values($values); |
220 | - $distinctVals= array_keys($valueCounts); |
|
220 | + $distinctVals = array_keys($valueCounts); |
|
221 | 221 | |
222 | 222 | $split = null; |
223 | 223 | |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | $wrong = 0.0; |
277 | 277 | $prob = []; |
278 | 278 | $leftLabel = $this->binaryLabels[0]; |
279 | - $rightLabel= $this->binaryLabels[1]; |
|
279 | + $rightLabel = $this->binaryLabels[1]; |
|
280 | 280 | |
281 | 281 | foreach ($values as $index => $value) { |
282 | 282 | if ($this->evaluate($value, $operator, $threshold)) { |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | // Calculate probabilities: Proportion of labels in each leaf |
300 | 300 | $dist = array_combine($this->binaryLabels, array_fill(0, 2, 0.0)); |
301 | 301 | foreach ($prob as $leaf => $counts) { |
302 | - $leafTotal = (float)array_sum($prob[$leaf]); |
|
302 | + $leafTotal = (float) array_sum($prob[$leaf]); |
|
303 | 303 | foreach ($counts as $label => $count) { |
304 | 304 | if (strval($leaf) == strval($label)) { |
305 | 305 | $dist[$leaf] = $count / $leafTotal; |
@@ -357,8 +357,8 @@ discard block |
||
357 | 357 | */ |
358 | 358 | public function __toString() |
359 | 359 | { |
360 | - return "IF $this->column $this->operator $this->value " . |
|
361 | - "THEN " . $this->binaryLabels[0] . " ". |
|
362 | - "ELSE " . $this->binaryLabels[1]; |
|
360 | + return "IF $this->column $this->operator $this->value ". |
|
361 | + "THEN ".$this->binaryLabels[0]." ". |
|
362 | + "ELSE ".$this->binaryLabels[1]; |
|
363 | 363 | } |
364 | 364 | } |