@@ -6,10 +6,8 @@ |
||
| 6 | 6 | |
| 7 | 7 | use Phpml\Helper\Predictable; |
| 8 | 8 | use Phpml\Helper\Trainable; |
| 9 | -use Phpml\Math\Statistic\Mean; |
|
| 10 | 9 | use Phpml\Classification\Classifier; |
| 11 | 10 | use Phpml\Classification\DecisionTree; |
| 12 | -use Phpml\Classification\NaiveBayes; |
|
| 13 | 11 | |
| 14 | 12 | class Bagging implements Classifier |
| 15 | 13 | { |
@@ -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 | |
@@ -118,13 +118,13 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | protected function getRandomSubset($index) |
| 120 | 120 | { |
| 121 | - $subsetLength = (int)ceil(sqrt($this->numSamples)); |
|
| 121 | + $subsetLength = (int) ceil(sqrt($this->numSamples)); |
|
| 122 | 122 | $denom = $this->subsetRatio / 2; |
| 123 | 123 | $subsetLength = $this->numSamples / (1 / $denom); |
| 124 | 124 | $index = $index * $subsetLength % $this->numSamples; |
| 125 | 125 | $samples = []; |
| 126 | 126 | $targets = []; |
| 127 | - for ($i=0; $i<$subsetLength * 2; $i++) { |
|
| 127 | + for ($i = 0; $i < $subsetLength * 2; $i++) { |
|
| 128 | 128 | $rand = rand($index, $this->numSamples - 1); |
| 129 | 129 | $samples[] = $this->samples[$rand]; |
| 130 | 130 | $targets[] = $this->targets[$rand]; |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | protected function initClassifiers() |
| 139 | 139 | { |
| 140 | 140 | $classifiers = []; |
| 141 | - for ($i=0; $i<$this->numClassifier; $i++) { |
|
| 141 | + for ($i = 0; $i < $this->numClassifier; $i++) { |
|
| 142 | 142 | $ref = new \ReflectionClass($this->classifier); |
| 143 | 143 | if ($this->classifierOptions) { |
| 144 | 144 | $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\Classification\DecisionTree; |
| 6 | 6 | |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | /** |
| 26 | 26 | * @var DecisionTreeLeaf |
| 27 | 27 | */ |
| 28 | - public $rightLeaf= null; |
|
| 28 | + public $rightLeaf = null; |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * @var array |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | $recordField = $record[$this->columnIndex]; |
| 65 | 65 | if (is_string($this->value) && preg_match("/^([<>=]{1,2})\s*(.*)/", $this->value, $matches)) { |
| 66 | 66 | $op = $matches[1]; |
| 67 | - $value= floatval($matches[2]); |
|
| 67 | + $value = floatval($matches[2]); |
|
| 68 | 68 | $recordField = strval($recordField); |
| 69 | 69 | eval("\$result = $recordField $op $value;"); |
| 70 | 70 | return $result; |
@@ -79,25 +79,25 @@ discard block |
||
| 79 | 79 | } else { |
| 80 | 80 | $value = $this->value; |
| 81 | 81 | $col = "col_$this->columnIndex"; |
| 82 | - if (! preg_match("/^[<>=]{1,2}/", $value)) { |
|
| 82 | + if (!preg_match("/^[<>=]{1,2}/", $value)) { |
|
| 83 | 83 | $value = "=$value"; |
| 84 | 84 | } |
| 85 | - $value = "<b>$col $value</b><br>Gini: ". number_format($this->giniIndex, 2); |
|
| 85 | + $value = "<b>$col $value</b><br>Gini: ".number_format($this->giniIndex, 2); |
|
| 86 | 86 | } |
| 87 | 87 | $str = "<table ><tr><td colspan=3 align=center style='border:1px solid;'> |
| 88 | 88 | $value</td></tr>"; |
| 89 | 89 | if ($this->leftLeaf || $this->rightLeaf) { |
| 90 | - $str .='<tr>'; |
|
| 90 | + $str .= '<tr>'; |
|
| 91 | 91 | if ($this->leftLeaf) { |
| 92 | - $str .="<td valign=top><b>| Yes</b><br>$this->leftLeaf</td>"; |
|
| 92 | + $str .= "<td valign=top><b>| Yes</b><br>$this->leftLeaf</td>"; |
|
| 93 | 93 | } else { |
| 94 | - $str .='<td></td>'; |
|
| 94 | + $str .= '<td></td>'; |
|
| 95 | 95 | } |
| 96 | - $str .='<td> </td>'; |
|
| 96 | + $str .= '<td> </td>'; |
|
| 97 | 97 | if ($this->rightLeaf) { |
| 98 | - $str .="<td valign=top align=right><b>No |</b><br>$this->rightLeaf</td>"; |
|
| 98 | + $str .= "<td valign=top align=right><b>No |</b><br>$this->rightLeaf</td>"; |
|
| 99 | 99 | } else { |
| 100 | - $str .='<td></td>'; |
|
| 100 | + $str .= '<td></td>'; |
|
| 101 | 101 | } |
| 102 | 102 | $str .= '</tr>'; |
| 103 | 103 | } |
@@ -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 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | protected function getColumnTypes(array $samples) |
| 82 | 82 | { |
| 83 | 83 | $types = []; |
| 84 | - for ($i=0; $i<$this->featureCount; $i++) { |
|
| 84 | + for ($i = 0; $i < $this->featureCount; $i++) { |
|
| 85 | 85 | $values = array_column($samples, $i); |
| 86 | 86 | $isCategorical = $this->isCategoricalColumn($values); |
| 87 | 87 | $types[] = $isCategorical ? self::NOMINAL : self::CONTINUOS; |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | $this->actualDepth = $depth; |
| 102 | 102 | } |
| 103 | 103 | $leftRecords = []; |
| 104 | - $rightRecords= []; |
|
| 104 | + $rightRecords = []; |
|
| 105 | 105 | $remainingTargets = []; |
| 106 | 106 | $prevRecord = null; |
| 107 | 107 | $allSame = true; |
@@ -114,10 +114,10 @@ discard block |
||
| 114 | 114 | if ($split->evaluate($record)) { |
| 115 | 115 | $leftRecords[] = $recordNo; |
| 116 | 116 | } else { |
| 117 | - $rightRecords[]= $recordNo; |
|
| 117 | + $rightRecords[] = $recordNo; |
|
| 118 | 118 | } |
| 119 | 119 | $target = $this->targets[$recordNo]; |
| 120 | - if (! in_array($target, $remainingTargets)) { |
|
| 120 | + if (!in_array($target, $remainingTargets)) { |
|
| 121 | 121 | $remainingTargets[] = $target; |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | $split->leftLeaf = $this->getSplitLeaf($leftRecords, $depth + 1); |
| 133 | 133 | } |
| 134 | 134 | if ($rightRecords) { |
| 135 | - $split->rightLeaf= $this->getSplitLeaf($rightRecords, $depth + 1); |
|
| 135 | + $split->rightLeaf = $this->getSplitLeaf($rightRecords, $depth + 1); |
|
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | return $split; |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $countMatrix[$label][$rowIndex]++; |
| 211 | 211 | } |
| 212 | 212 | $giniParts = [0, 0]; |
| 213 | - for ($i=0; $i<=1; $i++) { |
|
| 213 | + for ($i = 0; $i <= 1; $i++) { |
|
| 214 | 214 | $part = 0; |
| 215 | 215 | $sum = array_sum(array_column($countMatrix, $i)); |
| 216 | 216 | if ($sum > 0) { |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | // Detect and convert continuous data column values into |
| 233 | 233 | // discrete values by using the median as a threshold value |
| 234 | 234 | $columns = []; |
| 235 | - for ($i=0; $i<$this->featureCount; $i++) { |
|
| 235 | + for ($i = 0; $i < $this->featureCount; $i++) { |
|
| 236 | 236 | $values = array_column($samples, $i); |
| 237 | 237 | if ($this->columnTypes[$i] == self::CONTINUOS) { |
| 238 | 238 | $median = Mean::median($values); |
@@ -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) { |