1 | <?php |
||
12 | class NaiveBayes implements Classifier |
||
13 | { |
||
14 | use Trainable, Predictable; |
||
15 | |||
16 | const CONTINUOS = 1; |
||
17 | const NOMINAL = 2; |
||
18 | const EPSILON = 1e-10; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $std = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $mean= []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $discreteProb = []; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $dataType = []; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $p = []; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $sampleCount = 0; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $featureCount = 0; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $labels = []; |
||
59 | |||
60 | /** |
||
61 | * @param array $samples |
||
62 | * @param array $targets |
||
63 | */ |
||
64 | public function train(array $samples, array $targets) |
||
79 | |||
80 | /** |
||
81 | * Calculates vital statistics for each label & feature. Stores these |
||
82 | * values in private array in order to avoid repeated calculation |
||
83 | * @param string $label |
||
84 | * @param array $samples |
||
85 | */ |
||
86 | private function calculateStatistics($label, $samples) |
||
113 | |||
114 | /** |
||
115 | * Calculates the probability P(label|sample_n) |
||
116 | * |
||
117 | * @param array $sample |
||
118 | * @param int $feature |
||
119 | * @param string $label |
||
120 | * @return float |
||
121 | */ |
||
122 | private function sampleProbability($sample, $feature, $label) |
||
145 | |||
146 | /** |
||
147 | * Return samples belonging to specific label |
||
148 | * @param string $label |
||
149 | * @return array |
||
150 | */ |
||
151 | private function getSamplesByLabel($label) |
||
161 | |||
162 | /** |
||
163 | * @param array $sample |
||
164 | * @return mixed |
||
165 | */ |
||
166 | protected function predictSample(array $sample) |
||
184 | } |
||
185 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.