Completed
Pull Request — master (#30)
by
unknown
02:25
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();
@@ -32,7 +32,7 @@  discard block
 block discarded – undo
32 32
 		// Get distinct targets
33 33
 		$this->labels = $targets;
34 34
 		array_unique($this->labels);
35
-		foreach($this->labels as $label)
35
+		foreach ($this->labels as $label)
36 36
 		{
37 37
 			$samples = $this->getSamplesByLabel($label);
38 38
 			$this->p[$label] = count($samples) / $this->sampleCount;
@@ -49,10 +49,10 @@  discard block
 block discarded – undo
49 49
 	private function calculateStatistics($label, $samples)
50 50
 	{
51 51
 		$this->std[$label] = array_fill(0, $this->featureCount, 0);
52
-		$this->mean[$label]= array_fill(0, $this->featureCount, 0);
52
+		$this->mean[$label] = array_fill(0, $this->featureCount, 0);
53 53
 		$this->dataType[$label] = array_fill(0, $this->featureCount, self::CONTINUOS);
54 54
 		$this->discreteProb[$label] = array_fill(0, $this->featureCount, self::CONTINUOS);
55
-		for($i=0; $i<$this->featureCount; $i++)
55
+		for ($i = 0; $i < $this->featureCount; $i++)
56 56
 		{
57 57
 			// Get the values of nth column in the samples array
58 58
 			// Mean::arithmetic is called twice, can be optimized
@@ -90,19 +90,19 @@  discard block
 block discarded – undo
90 90
 		$value = $sample[$feature];
91 91
 		if ($this->dataType[$label][$feature] == self::NOMINAL)
92 92
 		{
93
-			if (! isset($this->discreteProb[$label][$feature][$value]) ||
93
+			if (!isset($this->discreteProb[$label][$feature][$value]) ||
94 94
 				$this->discreteProb[$label][$feature][$value] == 0)
95 95
 				return self::SMALL_VALUE;
96 96
 			return $this->discreteProb[$label][$feature][$value];
97 97
 		}
98
-		$std = $this->std[$label][$feature] ;
99
-		$mean= $this->mean[$label][$feature];
98
+		$std = $this->std[$label][$feature];
99
+		$mean = $this->mean[$label][$feature];
100 100
 		$std2 = $std * $std;
101 101
 		if ($std2 == 0)
102 102
 			$std2 = self::SMALL_VALUE;
103 103
 		// Calculate the probability density by use of normal/Gaussian distribution
104 104
 		// Ref: https://en.wikipedia.org/wiki/Normal_distribution
105
-		return (1 / sqrt(2 * $std2 * pi())) * exp( - pow($value - $mean, 2) / (2 * $std2));
105
+		return (1 / sqrt(2 * $std2 * pi())) * exp( -pow($value - $mean, 2) / (2 * $std2));
106 106
 	}
107 107
 
108 108
 	/**
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
 	private function getSamplesByLabel($label)
114 114
 	{
115 115
 		$samples = array();
116
-		for($i=0; $i<$this->sampleCount; $i++)
116
+		for ($i = 0; $i < $this->sampleCount; $i++)
117 117
 			if ($this->targets[$i] == $label)
118 118
 				$samples[] = $this->samples[$i];
119 119
 		return $samples;
@@ -129,10 +129,10 @@  discard block
 block discarded – undo
129 129
 		//	P(label|features) = P(label) * P(feature0|label) * P(feature1|label) .... P(featureN|label)
130 130
 		// Then compare probability for each class to determine which is most likely
131 131
 		$predictions = array();
132
-		foreach($this->labels as $label)
132
+		foreach ($this->labels as $label)
133 133
 		{
134 134
 			$p = $this->p[$label];
135
-			for($i=0; $i<$this->featureCount; $i++)
135
+			for ($i = 0; $i < $this->featureCount; $i++)
136 136
 			{
137 137
 				$Plf = $this->sampleProbability($sample, $i, $label);
138 138
 				// Correct the value for small and zero values
Please login to merge, or discard this patch.