@@ -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 | |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | $targets = []; |
141 | 141 | srand($index); |
142 | 142 | $bootstrapSize = $this->subsetRatio * $this->numSamples; |
143 | - for ($i=0; $i < $bootstrapSize; $i++) { |
|
143 | + for ($i = 0; $i < $bootstrapSize; $i++) { |
|
144 | 144 | $rand = rand(0, $this->numSamples - 1); |
145 | 145 | $samples[] = $this->samples[$rand]; |
146 | 146 | $targets[] = $this->targets[$rand]; |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | protected function initClassifiers() |
155 | 155 | { |
156 | 156 | $classifiers = []; |
157 | - for ($i=0; $i<$this->numClassifier; $i++) { |
|
157 | + for ($i = 0; $i < $this->numClassifier; $i++) { |
|
158 | 158 | $ref = new \ReflectionClass($this->classifier); |
159 | 159 | if ($this->classifierOptions) { |
160 | 160 | $obj = $ref->newInstanceArgs($this->classifierOptions); |
@@ -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); |
@@ -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; |
@@ -100,16 +100,16 @@ discard block |
||
100 | 100 | return 0.0; |
101 | 101 | } |
102 | 102 | |
103 | - $nodeSampleCount = (float)count($this->records); |
|
103 | + $nodeSampleCount = (float) count($this->records); |
|
104 | 104 | $iT = $this->giniIndex; |
105 | 105 | |
106 | 106 | if ($this->leftLeaf) { |
107 | - $pL = count($this->leftLeaf->records)/$nodeSampleCount; |
|
107 | + $pL = count($this->leftLeaf->records) / $nodeSampleCount; |
|
108 | 108 | $iT -= $pL * $this->leftLeaf->giniIndex; |
109 | 109 | } |
110 | 110 | |
111 | 111 | if ($this->rightLeaf) { |
112 | - $pR = count($this->rightLeaf->records)/$nodeSampleCount; |
|
112 | + $pR = count($this->rightLeaf->records) / $nodeSampleCount; |
|
113 | 113 | $iT -= $pR * $this->rightLeaf->giniIndex; |
114 | 114 | } |
115 | 115 | |
@@ -133,25 +133,25 @@ discard block |
||
133 | 133 | } else { |
134 | 134 | $col = "col_$this->columnIndex"; |
135 | 135 | } |
136 | - if (! preg_match("/^[<>=]{1,2}/", $value)) { |
|
136 | + if (!preg_match("/^[<>=]{1,2}/", $value)) { |
|
137 | 137 | $value = "=$value"; |
138 | 138 | } |
139 | - $value = "<b>$col $value</b><br>Gini: ". number_format($this->giniIndex, 2); |
|
139 | + $value = "<b>$col $value</b><br>Gini: ".number_format($this->giniIndex, 2); |
|
140 | 140 | } |
141 | 141 | $str = "<table ><tr><td colspan=3 align=center style='border:1px solid;'> |
142 | 142 | $value</td></tr>"; |
143 | 143 | if ($this->leftLeaf || $this->rightLeaf) { |
144 | - $str .='<tr>'; |
|
144 | + $str .= '<tr>'; |
|
145 | 145 | if ($this->leftLeaf) { |
146 | - $str .="<td valign=top><b>| Yes</b><br>" . $this->leftLeaf->getHTML($columnNames) . "</td>"; |
|
146 | + $str .= "<td valign=top><b>| Yes</b><br>".$this->leftLeaf->getHTML($columnNames)."</td>"; |
|
147 | 147 | } else { |
148 | - $str .='<td></td>'; |
|
148 | + $str .= '<td></td>'; |
|
149 | 149 | } |
150 | - $str .='<td> </td>'; |
|
150 | + $str .= '<td> </td>'; |
|
151 | 151 | if ($this->rightLeaf) { |
152 | - $str .="<td valign=top align=right><b>No |</b><br>" . $this->rightLeaf->getHTML($columnNames) . "</td>"; |
|
152 | + $str .= "<td valign=top align=right><b>No |</b><br>".$this->rightLeaf->getHTML($columnNames)."</td>"; |
|
153 | 153 | } else { |
154 | - $str .='<td></td>'; |
|
154 | + $str .= '<td></td>'; |
|
155 | 155 | } |
156 | 156 | $str .= '</tr>'; |
157 | 157 | } |
@@ -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 | } |
@@ -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; |
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\Math\Statistic; |
6 | 6 | |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | // Ref: https://en.wikipedia.org/wiki/Normal_distribution |
40 | 40 | $std2 = $this->std ** 2; |
41 | 41 | $mean = $this->mean; |
42 | - return exp(- (($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * pi()); |
|
42 | + return exp(-(($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * pi()); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -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\Helper; |
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; |
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::CONTINUOUS; |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | // otherwise group the records so that we can classify the leaf |
141 | 141 | // in case maximum depth is reached |
142 | 142 | $leftRecords = []; |
143 | - $rightRecords= []; |
|
143 | + $rightRecords = []; |
|
144 | 144 | $remainingTargets = []; |
145 | 145 | $prevRecord = null; |
146 | 146 | $allSame = true; |
@@ -158,12 +158,12 @@ discard block |
||
158 | 158 | if ($split->evaluate($record)) { |
159 | 159 | $leftRecords[] = $recordNo; |
160 | 160 | } else { |
161 | - $rightRecords[]= $recordNo; |
|
161 | + $rightRecords[] = $recordNo; |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | // Group remaining targets |
165 | 165 | $target = $this->targets[$recordNo]; |
166 | - if (! array_key_exists($target, $remainingTargets)) { |
|
166 | + if (!array_key_exists($target, $remainingTargets)) { |
|
167 | 167 | $remainingTargets[$target] = 1; |
168 | 168 | } else { |
169 | 169 | $remainingTargets[$target]++; |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | $split->leftLeaf = $this->getSplitLeaf($leftRecords, $depth + 1); |
180 | 180 | } |
181 | 181 | if ($rightRecords) { |
182 | - $split->rightLeaf= $this->getSplitLeaf($rightRecords, $depth + 1); |
|
182 | + $split->rightLeaf = $this->getSplitLeaf($rightRecords, $depth + 1); |
|
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | protected function getSelectedFeatures() : array |
252 | 252 | { |
253 | 253 | $allFeatures = range(0, $this->featureCount - 1); |
254 | - if ($this->numUsableFeatures === 0 && ! $this->selectedFeatures) { |
|
254 | + if ($this->numUsableFeatures === 0 && !$this->selectedFeatures) { |
|
255 | 255 | return $allFeatures; |
256 | 256 | } |
257 | 257 | |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | $countMatrix[$label][$rowIndex]++; |
289 | 289 | } |
290 | 290 | $giniParts = [0, 0]; |
291 | - for ($i=0; $i<=1; $i++) { |
|
291 | + for ($i = 0; $i <= 1; $i++) { |
|
292 | 292 | $part = 0; |
293 | 293 | $sum = array_sum(array_column($countMatrix, $i)); |
294 | 294 | if ($sum > 0) { |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | // Detect and convert continuous data column values into |
312 | 312 | // discrete values by using the median as a threshold value |
313 | 313 | $columns = []; |
314 | - for ($i=0; $i<$this->featureCount; $i++) { |
|
314 | + for ($i = 0; $i < $this->featureCount; $i++) { |
|
315 | 315 | $values = array_column($samples, $i); |
316 | 316 | if ($this->columnTypes[$i] == self::CONTINUOUS) { |
317 | 317 | $median = Mean::median($values); |
@@ -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; |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | protected function gradient(array $theta) |
64 | 64 | { |
65 | 65 | $costs = []; |
66 | - $gradient= []; |
|
66 | + $gradient = []; |
|
67 | 67 | $totalPenalty = 0; |
68 | 68 | |
69 | 69 | foreach ($this->samples as $index => $sample) { |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | list($cost, $grad, $penalty) = array_pad($result, 3, 0); |
74 | 74 | |
75 | 75 | $costs[] = $cost; |
76 | - $gradient[]= $grad; |
|
76 | + $gradient[] = $grad; |
|
77 | 77 | $totalPenalty += $penalty; |
78 | 78 | } |
79 | 79 | |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | protected function updateWeightsWithUpdates(array $updates, float $penalty) |
90 | 90 | { |
91 | 91 | // Updates all weights at once |
92 | - for ($i=0; $i <= $this->dimensions; $i++) { |
|
92 | + for ($i = 0; $i <= $this->dimensions; $i++) { |
|
93 | 93 | if ($i == 0) { |
94 | 94 | $this->theta[0] -= $this->learningRate * array_sum($updates); |
95 | 95 | } else { |