@@ -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 | } |
@@ -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 | |
@@ -9,12 +9,12 @@ discard block |
||
9 | 9 | /** |
10 | 10 | * Batch training is the default Adaline training algorithm |
11 | 11 | */ |
12 | - const BATCH_TRAINING = 1; |
|
12 | + const BATCH_TRAINING = 1; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * Online training: Stochastic gradient descent learning |
16 | 16 | */ |
17 | - const ONLINE_TRAINING = 2; |
|
17 | + const ONLINE_TRAINING = 2; |
|
18 | 18 | |
19 | 19 | /** |
20 | 20 | * Training type may be either 'Batch' or 'Online' learning |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | protected function runTraining(array $samples, array $targets) |
62 | 62 | { |
63 | 63 | // The cost function is the sum of squares |
64 | - $callback = function ($weights, $sample, $target) { |
|
64 | + $callback = function($weights, $sample, $target) { |
|
65 | 65 | $this->weights = $weights; |
66 | 66 | |
67 | 67 | $output = $this->output($sample); |
@@ -99,7 +99,7 @@ |
||
99 | 99 | $this->trainByLabel($samples, $targets, $labels); |
100 | 100 | } |
101 | 101 | |
102 | - /** |
|
102 | + /** |
|
103 | 103 | * @param array $samples |
104 | 104 | * @param array $targets |
105 | 105 | * @param array $labels |
@@ -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 | |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | protected function runTraining(array $samples, array $targets) |
171 | 171 | { |
172 | 172 | // The cost function is the sum of squares |
173 | - $callback = function ($weights, $sample, $target) { |
|
173 | + $callback = function($weights, $sample, $target) { |
|
174 | 174 | $this->weights = $weights; |
175 | 175 | |
176 | 176 | $prediction = $this->outputClass($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\Classification\Ensemble; |
6 | 6 | |
@@ -175,14 +175,14 @@ discard block |
||
175 | 175 | { |
176 | 176 | $weights = $this->weights; |
177 | 177 | $std = StandardDeviation::population($weights); |
178 | - $mean= Mean::arithmetic($weights); |
|
178 | + $mean = Mean::arithmetic($weights); |
|
179 | 179 | $min = min($weights); |
180 | - $minZ= (int)round(($min - $mean) / $std); |
|
180 | + $minZ = (int) round(($min - $mean) / $std); |
|
181 | 181 | |
182 | 182 | $samples = []; |
183 | 183 | $targets = []; |
184 | 184 | foreach ($weights as $index => $weight) { |
185 | - $z = (int)round(($weight - $mean) / $std) - $minZ + 1; |
|
185 | + $z = (int) round(($weight - $mean) / $std) - $minZ + 1; |
|
186 | 186 | for ($i = 0; $i < $z; ++$i) { |
187 | 187 | if (rand(0, 1) == 0) { |
188 | 188 | continue; |
@@ -264,6 +264,6 @@ discard block |
||
264 | 264 | $sum += $h * $alpha; |
265 | 265 | } |
266 | 266 | |
267 | - return $this->labels[ $sum > 0 ? 1 : -1]; |
|
267 | + return $this->labels[$sum > 0 ? 1 : -1]; |
|
268 | 268 | } |
269 | 269 | } |
@@ -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\Classification\Ensemble; |
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\Classification\Ensemble; |
6 | 6 | |
@@ -137,11 +137,11 @@ discard block |
||
137 | 137 | protected function initSingleClassifier($classifier) |
138 | 138 | { |
139 | 139 | if (is_float($this->featureSubsetRatio)) { |
140 | - $featureCount = (int)($this->featureSubsetRatio * $this->featureCount); |
|
140 | + $featureCount = (int) ($this->featureSubsetRatio * $this->featureCount); |
|
141 | 141 | } elseif ($this->featureCount == 'sqrt') { |
142 | - $featureCount = (int)sqrt($this->featureCount) + 1; |
|
142 | + $featureCount = (int) sqrt($this->featureCount) + 1; |
|
143 | 143 | } else { |
144 | - $featureCount = (int)log($this->featureCount, 2) + 1; |
|
144 | + $featureCount = (int) log($this->featureCount, 2) + 1; |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | if ($featureCount >= $this->featureCount) { |
@@ -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\Clustering; |
6 | 6 | |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $total += $val; |
102 | 102 | } |
103 | 103 | |
104 | - $this->membership[] = array_map(function ($val) use ($total) { |
|
104 | + $this->membership[] = array_map(function($val) use ($total) { |
|
105 | 105 | return $val / $total; |
106 | 106 | }, $row); |
107 | 107 | } |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | { |
219 | 219 | // Initialize variables, clusters and membership matrix |
220 | 220 | $this->sampleCount = count($samples); |
221 | - $this->samples =& $samples; |
|
221 | + $this->samples = & $samples; |
|
222 | 222 | $this->space = new Space(count($samples[0])); |
223 | 223 | $this->initClusters(); |
224 | 224 |
@@ -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 |
@@ -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 | |
@@ -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; |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | protected function gradient(array $theta) |
66 | 66 | { |
67 | 67 | $costs = []; |
68 | - $gradient= []; |
|
68 | + $gradient = []; |
|
69 | 69 | $totalPenalty = 0; |
70 | 70 | |
71 | 71 | foreach ($this->samples as $index => $sample) { |