@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | |
167 | 167 | /** |
168 | 168 | * @param array $records |
169 | - * @return DecisionTreeLeaf[] |
|
169 | + * @return null|DecisionTreeLeaf |
|
170 | 170 | */ |
171 | 171 | protected function getBestSplit($records) |
172 | 172 | { |
@@ -354,7 +354,6 @@ discard block |
||
354 | 354 | * each column in the given dataset. The importance values are |
355 | 355 | * normalized and their total makes 1.<br/> |
356 | 356 | * |
357 | - * @param array $labels |
|
358 | 357 | * @return array |
359 | 358 | */ |
360 | 359 | public function getFeatureImportances() |
@@ -394,7 +393,6 @@ discard block |
||
394 | 393 | * |
395 | 394 | * @param int $column |
396 | 395 | * @param DecisionTreeLeaf |
397 | - * @param array $collected |
|
398 | 396 | * |
399 | 397 | * @return array |
400 | 398 | */ |
@@ -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 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | { |
115 | 115 | $types = []; |
116 | 116 | $featureCount = count($samples[0]); |
117 | - for ($i=0; $i < $featureCount; $i++) { |
|
117 | + for ($i = 0; $i < $featureCount; $i++) { |
|
118 | 118 | $values = array_column($samples, $i); |
119 | 119 | $isCategorical = self::isCategoricalColumn($values); |
120 | 120 | $types[] = $isCategorical ? self::NOMINAL : self::CONTINUOS; |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | // otherwise group the records so that we can classify the leaf |
139 | 139 | // in case maximum depth is reached |
140 | 140 | $leftRecords = []; |
141 | - $rightRecords= []; |
|
141 | + $rightRecords = []; |
|
142 | 142 | $remainingTargets = []; |
143 | 143 | $prevRecord = null; |
144 | 144 | $allSame = true; |
@@ -156,12 +156,12 @@ discard block |
||
156 | 156 | if ($split->evaluate($record)) { |
157 | 157 | $leftRecords[] = $recordNo; |
158 | 158 | } else { |
159 | - $rightRecords[]= $recordNo; |
|
159 | + $rightRecords[] = $recordNo; |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | // Group remaining targets |
163 | 163 | $target = $this->targets[$recordNo]; |
164 | - if (! array_key_exists($target, $remainingTargets)) { |
|
164 | + if (!array_key_exists($target, $remainingTargets)) { |
|
165 | 165 | $remainingTargets[$target] = 1; |
166 | 166 | } else { |
167 | 167 | $remainingTargets[$target]++; |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | $split->leftLeaf = $this->getSplitLeaf($leftRecords, $depth + 1); |
178 | 178 | } |
179 | 179 | if ($rightRecords) { |
180 | - $split->rightLeaf= $this->getSplitLeaf($rightRecords, $depth + 1); |
|
180 | + $split->rightLeaf = $this->getSplitLeaf($rightRecords, $depth + 1); |
|
181 | 181 | } |
182 | 182 | } |
183 | 183 | return $split; |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | protected function getSelectedFeatures() |
248 | 248 | { |
249 | 249 | $allFeatures = range(0, $this->featureCount - 1); |
250 | - if ($this->numUsableFeatures == 0 && ! $this->selectedFeatures) { |
|
250 | + if ($this->numUsableFeatures == 0 && !$this->selectedFeatures) { |
|
251 | 251 | return $allFeatures; |
252 | 252 | } |
253 | 253 | |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | $countMatrix[$label][$rowIndex]++; |
284 | 284 | } |
285 | 285 | $giniParts = [0, 0]; |
286 | - for ($i=0; $i<=1; $i++) { |
|
286 | + for ($i = 0; $i <= 1; $i++) { |
|
287 | 287 | $part = 0; |
288 | 288 | $sum = array_sum(array_column($countMatrix, $i)); |
289 | 289 | if ($sum > 0) { |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | // Detect and convert continuous data column values into |
306 | 306 | // discrete values by using the median as a threshold value |
307 | 307 | $columns = []; |
308 | - for ($i=0; $i<$this->featureCount; $i++) { |
|
308 | + for ($i = 0; $i < $this->featureCount; $i++) { |
|
309 | 309 | $values = array_column($samples, $i); |
310 | 310 | if ($this->columnTypes[$i] == self::CONTINUOS) { |
311 | 311 | $median = Mean::median($values); |
@@ -75,7 +75,7 @@ |
||
75 | 75 | * |
76 | 76 | * Learning rate should be a float value between 0.0(exclusive) and 1.0(inclusive) <br> |
77 | 77 | * Maximum number of iterations can be an integer value greater than 0 |
78 | - * @param int $learningRate |
|
78 | + * @param double $learningRate |
|
79 | 79 | * @param int $maxIterations |
80 | 80 | */ |
81 | 81 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, |
@@ -5,7 +5,6 @@ |
||
5 | 5 | namespace Phpml\Classification\Linear; |
6 | 6 | |
7 | 7 | use Phpml\Helper\Predictable; |
8 | -use Phpml\Helper\Trainable; |
|
9 | 8 | use Phpml\Classification\Classifier; |
10 | 9 | use Phpml\Preprocessing\Normalizer; |
11 | 10 |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | protected static $errorFunction = 'outputClass'; |
23 | 23 | |
24 | - /** |
|
24 | + /** |
|
25 | 25 | * @var array |
26 | 26 | */ |
27 | 27 | protected $samples = []; |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | $this->threshold = $threshold; |
112 | 112 | } |
113 | 113 | |
114 | - /** |
|
114 | + /** |
|
115 | 115 | * @param array $samples |
116 | 116 | * @param array $targets |
117 | 117 | */ |
@@ -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 | |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | // Update bias |
170 | 170 | $this->weights[0] += $update * $this->learningRate; // Bias |
171 | 171 | // Update other weights |
172 | - for ($i=1; $i <= $this->featureCount; $i++) { |
|
172 | + for ($i = 1; $i <= $this->featureCount; $i++) { |
|
173 | 173 | $this->weights[$i] += $update * $sample[$i - 1] * $this->learningRate; |
174 | 174 | } |
175 | 175 | } |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | { |
203 | 203 | // Check for early stop: No change larger than 1e-5 |
204 | 204 | $diff = array_map( |
205 | - function ($w1, $w2) { |
|
205 | + function($w1, $w2) { |
|
206 | 206 | return abs($w1 - $w2) > 1e-5 ? 1 : 0; |
207 | 207 | }, |
208 | 208 | $oldWeights, $this->weights); |
@@ -259,6 +259,6 @@ discard block |
||
259 | 259 | |
260 | 260 | $predictedClass = $this->outputClass($sample); |
261 | 261 | |
262 | - return $this->labels[ $predictedClass ]; |
|
262 | + return $this->labels[$predictedClass]; |
|
263 | 263 | } |
264 | 264 | } |
@@ -1,4 +1,4 @@ |
||
1 | -<?php declare(strict_types=1); |
|
1 | +<?php declare(strict_types = 1); |
|
2 | 2 | |
3 | 3 | namespace Phpml\Classification; |
4 | 4 |
@@ -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 | |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $labels = array_count_values($this->targets); |
84 | 84 | $this->labels = array_keys($labels); |
85 | 85 | if (count($this->labels) != 2) { |
86 | - throw new \Exception("DecisionStump can classify between two classes only:" . implode(',', $this->labels)); |
|
86 | + throw new \Exception("DecisionStump can classify between two classes only:".implode(',', $this->labels)); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | // If a column index is given, it should be among the existing columns |
@@ -160,8 +160,8 @@ discard block |
||
160 | 160 | } |
161 | 161 | |
162 | 162 | // Try other possible points one by one |
163 | - for ($step = $minValue; $step <= $maxValue; $step+= $stepSize) { |
|
164 | - $threshold = (float)$step; |
|
163 | + for ($step = $minValue; $step <= $maxValue; $step += $stepSize) { |
|
164 | + $threshold = (float) $step; |
|
165 | 165 | $errorRate = $this->calculateErrorRate($threshold, $operator, $values); |
166 | 166 | if ($errorRate < $split['trainingErrorRate']) { |
167 | 167 | $split = ['value' => $threshold, 'operator' => $operator, |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | { |
184 | 184 | $values = array_column($this->samples, $col); |
185 | 185 | $valueCounts = array_count_values($values); |
186 | - $distinctVals= array_keys($valueCounts); |
|
186 | + $distinctVals = array_keys($valueCounts); |
|
187 | 187 | |
188 | 188 | $split = null; |
189 | 189 | |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | $total = (float) array_sum($this->weights); |
239 | 239 | $wrong = 0.0; |
240 | 240 | $leftLabel = $this->labels[0]; |
241 | - $rightLabel= $this->labels[1]; |
|
241 | + $rightLabel = $this->labels[1]; |
|
242 | 242 | foreach ($values as $index => $value) { |
243 | 243 | if ($this->evaluate($threshold, $operator, $value)) { |
244 | 244 | $predicted = $leftLabel; |
@@ -226,7 +226,7 @@ |
||
226 | 226 | /** |
227 | 227 | * |
228 | 228 | * @param type $leftValue |
229 | - * @param type $operator |
|
229 | + * @param string $operator |
|
230 | 230 | * @param type $rightValue |
231 | 231 | * |
232 | 232 | * @return boolean |
@@ -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 | |
@@ -173,15 +173,15 @@ discard block |
||
173 | 173 | { |
174 | 174 | $weights = $this->weights; |
175 | 175 | $std = StandardDeviation::population($weights); |
176 | - $mean= Mean::arithmetic($weights); |
|
176 | + $mean = Mean::arithmetic($weights); |
|
177 | 177 | $min = min($weights); |
178 | - $minZ= (int)round(($min - $mean) / $std); |
|
178 | + $minZ = (int) round(($min - $mean) / $std); |
|
179 | 179 | |
180 | 180 | $samples = []; |
181 | 181 | $targets = []; |
182 | 182 | foreach ($weights as $index => $weight) { |
183 | - $z = (int)round(($weight - $mean) / $std) - $minZ + 1; |
|
184 | - for ($i=0; $i < $z; $i++) { |
|
183 | + $z = (int) round(($weight - $mean) / $std) - $minZ + 1; |
|
184 | + for ($i = 0; $i < $z; $i++) { |
|
185 | 185 | if (rand(0, 1) == 0) { |
186 | 186 | continue; |
187 | 187 | } |
@@ -260,6 +260,6 @@ discard block |
||
260 | 260 | $sum += $h * $alpha; |
261 | 261 | } |
262 | 262 | |
263 | - return $this->labels[ $sum > 0 ? 1 : -1]; |
|
263 | + return $this->labels[$sum > 0 ? 1 : -1]; |
|
264 | 264 | } |
265 | 265 | } |