@@ -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\Classification\DecisionTree; |
6 | 6 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | /** |
35 | 35 | * @var DecisionTreeLeaf |
36 | 36 | */ |
37 | - public $rightLeaf= null; |
|
37 | + public $rightLeaf = null; |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * @var array |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | |
80 | 80 | if ($this->isContinuous) { |
81 | 81 | $op = $this->operator; |
82 | - $value= $this->numericValue; |
|
82 | + $value = $this->numericValue; |
|
83 | 83 | $recordField = strval($recordField); |
84 | 84 | eval("\$result = $recordField $op $value;"); |
85 | 85 | return $result; |
@@ -102,16 +102,16 @@ discard block |
||
102 | 102 | return 0.0; |
103 | 103 | } |
104 | 104 | |
105 | - $nodeSampleCount = (float)count($this->records); |
|
105 | + $nodeSampleCount = (float) count($this->records); |
|
106 | 106 | $iT = $this->giniIndex; |
107 | 107 | |
108 | 108 | if ($this->leftLeaf) { |
109 | - $pL = count($this->leftLeaf->records)/$nodeSampleCount; |
|
109 | + $pL = count($this->leftLeaf->records) / $nodeSampleCount; |
|
110 | 110 | $iT -= $pL * $this->leftLeaf->giniIndex; |
111 | 111 | } |
112 | 112 | |
113 | 113 | if ($this->rightLeaf) { |
114 | - $pR = count($this->rightLeaf->records)/$nodeSampleCount; |
|
114 | + $pR = count($this->rightLeaf->records) / $nodeSampleCount; |
|
115 | 115 | $iT -= $pR * $this->rightLeaf->giniIndex; |
116 | 116 | } |
117 | 117 | |
@@ -138,22 +138,22 @@ discard block |
||
138 | 138 | if (!preg_match("/^[<>=]{1,2}/", $value)) { |
139 | 139 | $value = "=$value"; |
140 | 140 | } |
141 | - $value = "<b>$col $value</b><br>Gini: ". number_format($this->giniIndex, 2); |
|
141 | + $value = "<b>$col $value</b><br>Gini: ".number_format($this->giniIndex, 2); |
|
142 | 142 | } |
143 | 143 | $str = "<table ><tr><td colspan=3 align=center style='border:1px solid;'> |
144 | 144 | $value</td></tr>"; |
145 | 145 | if ($this->leftLeaf || $this->rightLeaf) { |
146 | - $str .='<tr>'; |
|
146 | + $str .= '<tr>'; |
|
147 | 147 | if ($this->leftLeaf) { |
148 | - $str .="<td valign=top><b>| Yes</b><br>" . $this->leftLeaf->getHTML($columnNames) . "</td>"; |
|
148 | + $str .= "<td valign=top><b>| Yes</b><br>".$this->leftLeaf->getHTML($columnNames)."</td>"; |
|
149 | 149 | } else { |
150 | - $str .='<td></td>'; |
|
150 | + $str .= '<td></td>'; |
|
151 | 151 | } |
152 | - $str .='<td> </td>'; |
|
152 | + $str .= '<td> </td>'; |
|
153 | 153 | if ($this->rightLeaf) { |
154 | - $str .="<td valign=top align=right><b>No |</b><br>" . $this->rightLeaf->getHTML($columnNames) . "</td>"; |
|
154 | + $str .= "<td valign=top align=right><b>No |</b><br>".$this->rightLeaf->getHTML($columnNames)."</td>"; |
|
155 | 155 | } else { |
156 | - $str .='<td></td>'; |
|
156 | + $str .= '<td></td>'; |
|
157 | 157 | } |
158 | 158 | $str .= '</tr>'; |
159 | 159 | } |
@@ -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 | |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | // otherwise group the records so that we can classify the leaf |
144 | 144 | // in case maximum depth is reached |
145 | 145 | $leftRecords = []; |
146 | - $rightRecords= []; |
|
146 | + $rightRecords = []; |
|
147 | 147 | $remainingTargets = []; |
148 | 148 | $prevRecord = null; |
149 | 149 | $allSame = true; |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | if ($split->evaluate($record)) { |
162 | 162 | $leftRecords[] = $recordNo; |
163 | 163 | } else { |
164 | - $rightRecords[]= $recordNo; |
|
164 | + $rightRecords[] = $recordNo; |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | // Group remaining targets |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | $split->leftLeaf = $this->getSplitLeaf($leftRecords, $depth + 1); |
183 | 183 | } |
184 | 184 | if ($rightRecords) { |
185 | - $split->rightLeaf= $this->getSplitLeaf($rightRecords, $depth + 1); |
|
185 | + $split->rightLeaf = $this->getSplitLeaf($rightRecords, $depth + 1); |
|
186 | 186 | } |
187 | 187 | } |
188 | 188 |
@@ -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 |