@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | |
| 184 | 184 | /** |
| 185 | 185 | * @param array $records |
| 186 | - * @return DecisionTreeLeaf[] |
|
| 186 | + * @return null|DecisionTreeLeaf |
|
| 187 | 187 | */ |
| 188 | 188 | protected function getBestSplit($records) |
| 189 | 189 | { |
@@ -359,7 +359,6 @@ discard block |
||
| 359 | 359 | /** |
| 360 | 360 | * Used to set predefined features to consider while deciding which column to use for a split, |
| 361 | 361 | * |
| 362 | - * @param array $features |
|
| 363 | 362 | */ |
| 364 | 363 | protected function setSelectedFeatures(array $selectedFeatures) |
| 365 | 364 | { |
@@ -397,7 +396,6 @@ discard block |
||
| 397 | 396 | * each column in the given dataset. The importance values are |
| 398 | 397 | * normalized and their total makes 1.<br/> |
| 399 | 398 | * |
| 400 | - * @param array $labels |
|
| 401 | 399 | * @return array |
| 402 | 400 | */ |
| 403 | 401 | public function getFeatureImportances() |
@@ -437,7 +435,6 @@ discard block |
||
| 437 | 435 | * |
| 438 | 436 | * @param int $column |
| 439 | 437 | * @param DecisionTreeLeaf |
| 440 | - * @param array $collected |
|
| 441 | 438 | * |
| 442 | 439 | * @return array |
| 443 | 440 | */ |
@@ -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 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | protected function getColumnTypes(array $samples) |
| 113 | 113 | { |
| 114 | 114 | $types = []; |
| 115 | - for ($i=0; $i<$this->featureCount; $i++) { |
|
| 115 | + for ($i = 0; $i < $this->featureCount; $i++) { |
|
| 116 | 116 | $values = array_column($samples, $i); |
| 117 | 117 | $isCategorical = $this->isCategoricalColumn($values); |
| 118 | 118 | $types[] = $isCategorical ? self::NOMINAL : self::CONTINUOS; |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | // otherwise group the records so that we can classify the leaf |
| 137 | 137 | // in case maximum depth is reached |
| 138 | 138 | $leftRecords = []; |
| 139 | - $rightRecords= []; |
|
| 139 | + $rightRecords = []; |
|
| 140 | 140 | $remainingTargets = []; |
| 141 | 141 | $prevRecord = null; |
| 142 | 142 | $allSame = true; |
@@ -154,12 +154,12 @@ discard block |
||
| 154 | 154 | if ($split->evaluate($record)) { |
| 155 | 155 | $leftRecords[] = $recordNo; |
| 156 | 156 | } else { |
| 157 | - $rightRecords[]= $recordNo; |
|
| 157 | + $rightRecords[] = $recordNo; |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Group remaining targets |
| 161 | 161 | $target = $this->targets[$recordNo]; |
| 162 | - if (! array_key_exists($target, $remainingTargets)) { |
|
| 162 | + if (!array_key_exists($target, $remainingTargets)) { |
|
| 163 | 163 | $remainingTargets[$target] = 1; |
| 164 | 164 | } else { |
| 165 | 165 | $remainingTargets[$target]++; |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | $split->leftLeaf = $this->getSplitLeaf($leftRecords, $depth + 1); |
| 176 | 176 | } |
| 177 | 177 | if ($rightRecords) { |
| 178 | - $split->rightLeaf= $this->getSplitLeaf($rightRecords, $depth + 1); |
|
| 178 | + $split->rightLeaf = $this->getSplitLeaf($rightRecords, $depth + 1); |
|
| 179 | 179 | } |
| 180 | 180 | } |
| 181 | 181 | return $split; |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | protected function getSelectedFeatures() |
| 235 | 235 | { |
| 236 | 236 | $allFeatures = range(0, $this->featureCount - 1); |
| 237 | - if ($this->numUsableFeatures == 0 && ! $this->selectedFeatures) { |
|
| 237 | + if ($this->numUsableFeatures == 0 && !$this->selectedFeatures) { |
|
| 238 | 238 | return $allFeatures; |
| 239 | 239 | } |
| 240 | 240 | |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | $countMatrix[$label][$rowIndex]++; |
| 271 | 271 | } |
| 272 | 272 | $giniParts = [0, 0]; |
| 273 | - for ($i=0; $i<=1; $i++) { |
|
| 273 | + for ($i = 0; $i <= 1; $i++) { |
|
| 274 | 274 | $part = 0; |
| 275 | 275 | $sum = array_sum(array_column($countMatrix, $i)); |
| 276 | 276 | if ($sum > 0) { |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | // Detect and convert continuous data column values into |
| 293 | 293 | // discrete values by using the median as a threshold value |
| 294 | 294 | $columns = []; |
| 295 | - for ($i=0; $i<$this->featureCount; $i++) { |
|
| 295 | + for ($i = 0; $i < $this->featureCount; $i++) { |
|
| 296 | 296 | $values = array_column($samples, $i); |
| 297 | 297 | if ($this->columnTypes[$i] == self::CONTINUOS) { |
| 298 | 298 | $median = Mean::median($values); |
@@ -52,7 +52,7 @@ |
||
| 52 | 52 | * If normalizeInputs is set to true, then every input given to the algorithm will be standardized |
| 53 | 53 | * by use of standard deviation and mean calculation |
| 54 | 54 | * |
| 55 | - * @param int $learningRate |
|
| 55 | + * @param double $learningRate |
|
| 56 | 56 | * @param int $maxIterations |
| 57 | 57 | */ |
| 58 | 58 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, |
@@ -4,9 +4,6 @@ |
||
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Classification\Linear; |
| 6 | 6 | |
| 7 | -use Phpml\Helper\Predictable; |
|
| 8 | -use Phpml\Helper\Trainable; |
|
| 9 | -use Phpml\Classification\Classifier; |
|
| 10 | 7 | use Phpml\Classification\Linear\Perceptron; |
| 11 | 8 | use Phpml\Preprocessing\Normalizer; |
| 12 | 9 | |
@@ -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 | |
@@ -16,12 +16,12 @@ discard block |
||
| 16 | 16 | /** |
| 17 | 17 | * Batch training is the default Adaline training algorithm |
| 18 | 18 | */ |
| 19 | - const BATCH_TRAINING = 1; |
|
| 19 | + const BATCH_TRAINING = 1; |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Online training: Stochastic gradient descent learning |
| 23 | 23 | */ |
| 24 | - const ONLINE_TRAINING = 2; |
|
| 24 | + const ONLINE_TRAINING = 2; |
|
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * The function whose result will be used to calculate the network error |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | $this->normalizer = new Normalizer(Normalizer::NORM_STD); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if (! in_array($trainingType, [self::BATCH_TRAINING, self::ONLINE_TRAINING])) { |
|
| 65 | + if (!in_array($trainingType, [self::BATCH_TRAINING, self::ONLINE_TRAINING])) { |
|
| 66 | 66 | throw new \Exception("Adaline can only be trained with batch and online/stochastic gradient descent algorithm"); |
| 67 | 67 | } |
| 68 | 68 | $this->trainingType = $trainingType; |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $sum = array_sum($updates); |
| 104 | 104 | |
| 105 | 105 | // Updates all weights at once |
| 106 | - for ($i=0; $i <= $this->featureCount; $i++) { |
|
| 106 | + for ($i = 0; $i <= $this->featureCount; $i++) { |
|
| 107 | 107 | if ($i == 0) { |
| 108 | 108 | $this->weights[0] += $this->learningRate * $sum; |
| 109 | 109 | } else { |
@@ -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\Preprocessing; |
| 6 | 6 | |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | { |
| 13 | 13 | const NORM_L1 = 1; |
| 14 | 14 | const NORM_L2 = 2; |
| 15 | - const NORM_STD= 3; |
|
| 15 | + const NORM_STD = 3; |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * @var int |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | foreach ($sample as $feature) { |
| 118 | 118 | $norm2 += $feature * $feature; |
| 119 | 119 | } |
| 120 | - $norm2 = sqrt((float)$norm2); |
|
| 120 | + $norm2 = sqrt((float) $norm2); |
|
| 121 | 121 | |
| 122 | 122 | if (0 == $norm2) { |
| 123 | 123 | $sample = array_fill(0, count($sample), 1); |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | protected static $errorFunction = 'outputClass'; |
| 22 | 22 | |
| 23 | - /** |
|
| 23 | + /** |
|
| 24 | 24 | * @var array |
| 25 | 25 | */ |
| 26 | 26 | protected $samples = []; |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | $this->maxIterations = $maxIterations; |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - /** |
|
| 81 | + /** |
|
| 82 | 82 | * @param array $samples |
| 83 | 83 | * @param array $targets |
| 84 | 84 | */ |
@@ -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 | |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | // Update bias |
| 124 | 124 | $this->weights[0] += $update * $this->learningRate; // Bias |
| 125 | 125 | // Update other weights |
| 126 | - for ($i=1; $i <= $this->featureCount; $i++) { |
|
| 126 | + for ($i = 1; $i <= $this->featureCount; $i++) { |
|
| 127 | 127 | $this->weights[$i] += $update * $sample[$i - 1] * $this->learningRate; |
| 128 | 128 | } |
| 129 | 129 | } |
@@ -169,6 +169,6 @@ discard block |
||
| 169 | 169 | { |
| 170 | 170 | $predictedClass = $this->outputClass($sample); |
| 171 | 171 | |
| 172 | - return $this->labels[ $predictedClass ]; |
|
| 172 | + return $this->labels[$predictedClass]; |
|
| 173 | 173 | } |
| 174 | 174 | } |
@@ -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\Linear; |
| 6 | 6 | |