Completed
Push — master ( cb5a99...e603d6 )
by Arkadiusz
06:54
created
src/Phpml/Classification/NaiveBayes.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types=1);
3
+declare(strict_types = 1);
4 4
 
5 5
 namespace Phpml\Classification;
6 6
 
@@ -12,11 +12,11 @@  discard block
 block discarded – undo
12 12
 class NaiveBayes implements Classifier
13 13
 {
14 14
     use Trainable, Predictable;
15
-    const CONTINUOS    = 1;
16
-    const NOMINAL    = 2;
15
+    const CONTINUOS = 1;
16
+    const NOMINAL = 2;
17 17
     const EPSILON = 1e-10;
18 18
     private $std = array();
19
-    private $mean= array();
19
+    private $mean = array();
20 20
     private $discreteProb = array();
21 21
     private $dataType = array();
22 22
     private $p = array();
@@ -48,10 +48,10 @@  discard block
 block discarded – undo
48 48
     private function calculateStatistics($label, $samples)
49 49
     {
50 50
         $this->std[$label] = array_fill(0, $this->featureCount, 0);
51
-        $this->mean[$label]= array_fill(0, $this->featureCount, 0);
51
+        $this->mean[$label] = array_fill(0, $this->featureCount, 0);
52 52
         $this->dataType[$label] = array_fill(0, $this->featureCount, self::CONTINUOS);
53 53
         $this->discreteProb[$label] = array_fill(0, $this->featureCount, self::CONTINUOS);
54
-        for ($i=0; $i<$this->featureCount; $i++) {
54
+        for ($i = 0; $i < $this->featureCount; $i++) {
55 55
             // Get the values of nth column in the samples array
56 56
             // Mean::arithmetic is called twice, can be optimized
57 57
             $values = array_column($samples, $i);
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
                 $this->dataType[$label][$i] = self::NOMINAL;
63 63
                 $this->discreteProb[$label][$i] = array_count_values($values);
64 64
                 $db = &$this->discreteProb[$label][$i];
65
-                $db = array_map(function ($el) use ($numValues) {
65
+                $db = array_map(function($el) use ($numValues) {
66 66
                     return $el / $numValues;
67 67
                 }, $db);
68 68
             } else {
@@ -84,14 +84,14 @@  discard block
 block discarded – undo
84 84
     {
85 85
         $value = $sample[$feature];
86 86
         if ($this->dataType[$label][$feature] == self::NOMINAL) {
87
-            if (! isset($this->discreteProb[$label][$feature][$value]) ||
87
+            if (!isset($this->discreteProb[$label][$feature][$value]) ||
88 88
                 $this->discreteProb[$label][$feature][$value] == 0) {
89 89
                 return self::EPSILON;
90 90
             }
91 91
             return $this->discreteProb[$label][$feature][$value];
92 92
         }
93
-        $std = $this->std[$label][$feature] ;
94
-        $mean= $this->mean[$label][$feature];
93
+        $std = $this->std[$label][$feature];
94
+        $mean = $this->mean[$label][$feature];
95 95
         // Calculate the probability density by use of normal/Gaussian distribution
96 96
         // Ref: https://en.wikipedia.org/wiki/Normal_distribution
97 97
 		//
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 		// some libraries adopt taking log of calculations such as
100 100
 		// scikit-learn did.
101 101
 		// (See : https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/naive_bayes.py)
102
-		$pdf  =  -0.5 * log(2.0 * pi() * $std * $std);
102
+		$pdf  = -0.5 * log(2.0 * pi() * $std * $std);
103 103
 		$pdf -= 0.5 * pow($value - $mean, 2) / ($std * $std);
104 104
 		return $pdf;
105 105
     }
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
     private function getSamplesByLabel($label)
113 113
     {
114 114
         $samples = array();
115
-        for ($i=0; $i<$this->sampleCount; $i++) {
115
+        for ($i = 0; $i < $this->sampleCount; $i++) {
116 116
             if ($this->targets[$i] == $label) {
117 117
                 $samples[] = $this->samples[$i];
118 118
             }
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
             $predictions = array();
140 140
             foreach ($this->labels as $label) {
141 141
                 $p = $this->p[$label];
142
-                for ($i=0; $i<$this->featureCount; $i++) {
142
+                for ($i = 0; $i < $this->featureCount; $i++) {
143 143
                     $Plf = $this->sampleProbability($sample, $i, $label);
144 144
                     $p += $Plf;
145 145
                 }
@@ -149,7 +149,7 @@  discard block
 block discarded – undo
149 149
             reset($predictions);
150 150
             $samplePredictions[] = key($predictions);
151 151
         }
152
-        if (! $isArray) {
152
+        if (!$isArray) {
153 153
             return $samplePredictions[0];
154 154
         }
155 155
         return $samplePredictions;
Please login to merge, or discard this patch.