@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -declare(strict_types=1); |
|
2 | +declare(strict_types = 1); |
|
3 | 3 | |
4 | 4 | namespace Phpml\Classification\Ensemble; |
5 | 5 | |
@@ -70,11 +70,11 @@ discard block |
||
70 | 70 | protected function initSingleClassifier($classifier, $index) |
71 | 71 | { |
72 | 72 | if (is_float($this->featureSubsetRatio)) { |
73 | - $featureCount = (int)($this->featureSubsetRatio * $this->featureCount); |
|
73 | + $featureCount = (int) ($this->featureSubsetRatio * $this->featureCount); |
|
74 | 74 | } elseif ($this->featureCount == 'sqrt') { |
75 | - $featureCount = (int)sqrt($this->featureCount) + 1; |
|
75 | + $featureCount = (int) sqrt($this->featureCount) + 1; |
|
76 | 76 | } else { |
77 | - $featureCount = (int)log($this->featureCount, 2) + 1; |
|
77 | + $featureCount = (int) log($this->featureCount, 2) + 1; |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | 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\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); |
@@ -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, |
@@ -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 | |
@@ -15,12 +15,12 @@ discard block |
||
15 | 15 | /** |
16 | 16 | * Batch training is the default Adaline training algorithm |
17 | 17 | */ |
18 | - const BATCH_TRAINING = 1; |
|
18 | + const BATCH_TRAINING = 1; |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Online training: Stochastic gradient descent learning |
22 | 22 | */ |
23 | - const ONLINE_TRAINING = 2; |
|
23 | + const ONLINE_TRAINING = 2; |
|
24 | 24 | |
25 | 25 | /** |
26 | 26 | * The function whose result will be used to calculate the network error |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, |
53 | 53 | bool $normalizeInputs = true, int $trainingType = self::BATCH_TRAINING) |
54 | 54 | { |
55 | - if (! in_array($trainingType, [self::BATCH_TRAINING, self::ONLINE_TRAINING])) { |
|
55 | + if (!in_array($trainingType, [self::BATCH_TRAINING, self::ONLINE_TRAINING])) { |
|
56 | 56 | throw new \Exception("Adaline can only be trained with batch and online/stochastic gradient descent algorithm"); |
57 | 57 | } |
58 | 58 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | protected function updateWeights(array $updates) |
105 | 105 | { |
106 | 106 | // Updates all weights at once |
107 | - for ($i=0; $i <= $this->featureCount; $i++) { |
|
107 | + for ($i = 0; $i <= $this->featureCount; $i++) { |
|
108 | 108 | if ($i == 0) { |
109 | 109 | $this->weights[0] += $this->learningRate * array_sum($updates); |
110 | 110 | } else { |
@@ -4,8 +4,6 @@ |
||
4 | 4 | |
5 | 5 | namespace Phpml\Classification\Linear; |
6 | 6 | |
7 | -use Phpml\Classification\Classifier; |
|
8 | - |
|
9 | 7 | class Adaline extends Perceptron |
10 | 8 | { |
11 | 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\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 | } |
@@ -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, |
@@ -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 | } |
@@ -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\Linear; |
6 | 6 | |
@@ -199,8 +199,8 @@ discard block |
||
199 | 199 | } |
200 | 200 | |
201 | 201 | // Try other possible points one by one |
202 | - for ($step = $minValue; $step <= $maxValue; $step+= $stepSize) { |
|
203 | - $threshold = (float)$step; |
|
202 | + for ($step = $minValue; $step <= $maxValue; $step += $stepSize) { |
|
203 | + $threshold = (float) $step; |
|
204 | 204 | list($errorRate, $prob) = $this->calculateErrorRate($threshold, $operator, $values); |
205 | 205 | if ($errorRate < $split['trainingErrorRate']) { |
206 | 206 | $split = ['value' => $threshold, 'operator' => $operator, |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | { |
224 | 224 | $values = array_column($this->samples, $col); |
225 | 225 | $valueCounts = array_count_values($values); |
226 | - $distinctVals= array_keys($valueCounts); |
|
226 | + $distinctVals = array_keys($valueCounts); |
|
227 | 227 | |
228 | 228 | $split = null; |
229 | 229 | |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | $wrong = 0.0; |
282 | 282 | $prob = []; |
283 | 283 | $leftLabel = $this->binaryLabels[0]; |
284 | - $rightLabel= $this->binaryLabels[1]; |
|
284 | + $rightLabel = $this->binaryLabels[1]; |
|
285 | 285 | |
286 | 286 | foreach ($values as $index => $value) { |
287 | 287 | if ($this->evaluate($value, $operator, $threshold)) { |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | $wrong += $this->weights[$index]; |
296 | 296 | } |
297 | 297 | |
298 | - if (! isset($prob[$predicted][$target])) { |
|
298 | + if (!isset($prob[$predicted][$target])) { |
|
299 | 299 | $prob[$predicted][$target] = 0; |
300 | 300 | } |
301 | 301 | $prob[$predicted][$target]++; |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | // Calculate probabilities: Proportion of labels in each leaf |
305 | 305 | $dist = array_combine($this->binaryLabels, array_fill(0, 2, 0.0)); |
306 | 306 | foreach ($prob as $leaf => $counts) { |
307 | - $leafTotal = (float)array_sum($prob[$leaf]); |
|
307 | + $leafTotal = (float) array_sum($prob[$leaf]); |
|
308 | 308 | foreach ($counts as $label => $count) { |
309 | 309 | if (strval($leaf) == strval($label)) { |
310 | 310 | $dist[$leaf] = $count / $leafTotal; |
@@ -355,8 +355,8 @@ discard block |
||
355 | 355 | */ |
356 | 356 | public function __toString() |
357 | 357 | { |
358 | - return "IF $this->column $this->operator $this->value " . |
|
359 | - "THEN " . $this->binaryLabels[0] . " ". |
|
360 | - "ELSE " . $this->binaryLabels[1]; |
|
358 | + return "IF $this->column $this->operator $this->value ". |
|
359 | + "THEN ".$this->binaryLabels[0]." ". |
|
360 | + "ELSE ".$this->binaryLabels[1]; |
|
361 | 361 | } |
362 | 362 | } |
@@ -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 |