@@ -69,8 +69,7 @@ discard block |
||
| 69 | 69 | $db = array_map(function($el) use ($numValues) { |
| 70 | 70 | return $el / $numValues; |
| 71 | 71 | }, $db); |
| 72 | - } |
|
| 73 | - else |
|
| 72 | + } else |
|
| 74 | 73 | { |
| 75 | 74 | $this->mean[$label][$i] = Mean::arithmetic($values); |
| 76 | 75 | $this->std[$label][$i] = StandardDeviation::population($values, false); |
@@ -91,15 +90,17 @@ discard block |
||
| 91 | 90 | if ($this->dataType[$label][$feature] == self::NOMINAL) |
| 92 | 91 | { |
| 93 | 92 | if (! isset($this->discreteProb[$label][$feature][$value]) || |
| 94 | - $this->discreteProb[$label][$feature][$value] == 0) |
|
| 95 | - return self::SMALL_VALUE; |
|
| 93 | + $this->discreteProb[$label][$feature][$value] == 0) { |
|
| 94 | + return self::SMALL_VALUE; |
|
| 95 | + } |
|
| 96 | 96 | return $this->discreteProb[$label][$feature][$value]; |
| 97 | 97 | } |
| 98 | 98 | $std = $this->std[$label][$feature] ; |
| 99 | 99 | $mean= $this->mean[$label][$feature]; |
| 100 | 100 | $std2 = $std * $std; |
| 101 | - if ($std2 == 0) |
|
| 102 | - $std2 = self::SMALL_VALUE; |
|
| 101 | + if ($std2 == 0) { |
|
| 102 | + $std2 = self::SMALL_VALUE; |
|
| 103 | + } |
|
| 103 | 104 | // Calculate the probability density by use of normal/Gaussian distribution |
| 104 | 105 | // Ref: https://en.wikipedia.org/wiki/Normal_distribution |
| 105 | 106 | return (1 / sqrt(2 * $std2 * pi())) * exp( - pow($value - $mean, 2) / (2 * $std2)); |
@@ -113,9 +114,10 @@ discard block |
||
| 113 | 114 | private function getSamplesByLabel($label) |
| 114 | 115 | { |
| 115 | 116 | $samples = array(); |
| 116 | - for($i=0; $i<$this->sampleCount; $i++) |
|
| 117 | - if ($this->targets[$i] == $label) |
|
| 117 | + for($i=0; $i<$this->sampleCount; $i++) { |
|
| 118 | + if ($this->targets[$i] == $label) |
|
| 118 | 119 | $samples[] = $this->samples[$i]; |
| 120 | + } |
|
| 119 | 121 | return $samples; |
| 120 | 122 | } |
| 121 | 123 | |
@@ -136,8 +138,9 @@ discard block |
||
| 136 | 138 | { |
| 137 | 139 | $Plf = $this->sampleProbability($sample, $i, $label); |
| 138 | 140 | // Correct the value for small and zero values |
| 139 | - if (is_nan($Plf) || $Plf < self::SMALL_VALUE) |
|
| 140 | - $Plf = self::SMALL_VALUE; |
|
| 141 | + if (is_nan($Plf) || $Plf < self::SMALL_VALUE) { |
|
| 142 | + $Plf = self::SMALL_VALUE; |
|
| 143 | + } |
|
| 141 | 144 | $p *= $Plf; |
| 142 | 145 | } |
| 143 | 146 | $predictions[$label] = $p; |