Completed
Pull Request — master (#30)
by
unknown
02:36
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 SMALL_VALUE = 1e-32;
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);
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
                 $this->dataType[$label][$i] = self::NOMINAL;
64 64
                 $this->discreteProb[$label][$i] = $freq;
65 65
                 $db = &$this->discreteProb[$label][$i];
66
-                $db = array_map(function ($el) use ($numValues) {
66
+                $db = array_map(function($el) use ($numValues) {
67 67
                     return $el / $numValues;
68 68
                 }, $db);
69 69
             } else {
@@ -84,21 +84,21 @@  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::SMALL_VALUE;
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
         $std2 = $std * $std;
96 96
         if ($std2 == 0) {
97 97
             $std2 = self::SMALL_VALUE;
98 98
         }
99 99
         // Calculate the probability density by use of normal/Gaussian distribution
100 100
         // Ref: https://en.wikipedia.org/wiki/Normal_distribution
101
-        return (1 / sqrt(2 * $std2 * pi())) * exp(- pow($value - $mean, 2) / (2 * $std2));
101
+        return (1 / sqrt(2 * $std2 * pi())) * exp(-pow($value - $mean, 2) / (2 * $std2));
102 102
     }
103 103
 
104 104
     /**
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
     private function getSamplesByLabel($label)
110 110
     {
111 111
         $samples = array();
112
-        for ($i=0; $i<$this->sampleCount; $i++) {
112
+        for ($i = 0; $i < $this->sampleCount; $i++) {
113 113
             if ($this->targets[$i] == $label) {
114 114
                 $samples[] = $this->samples[$i];
115 115
             }
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
             $predictions = array();
137 137
             foreach ($this->labels as $label) {
138 138
                 $p = $this->p[$label];
139
-                for ($i=0; $i<$this->featureCount; $i++) {
139
+                for ($i = 0; $i < $this->featureCount; $i++) {
140 140
                     $Plf = $this->sampleProbability($sample, $i, $label);
141 141
                     // Correct the value for small and zero values
142 142
                     if (is_nan($Plf) || $Plf < self::SMALL_VALUE) {
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
             reset($predictions);
151 151
             $samplePredictions[] = key($predictions);
152 152
         }
153
-        if (! $isArray) {
153
+        if (!$isArray) {
154 154
             return $samplePredictions[0];
155 155
         }
156 156
         return $samplePredictions;
Please login to merge, or discard this patch.