Completed
Push — develop ( 6f5f19...d2e0ce )
by Arkadiusz
02:17
created

NaiveBayes::predictSample()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Classifier;
6
7
use Phpml\Classifier\Traits\Predictable;
8
use Phpml\Classifier\Traits\Trainable;
9
10
class NaiveBayes implements Classifier
11
{
12
    use Trainable, Predictable;
13
14
    /**
15
     * @param array $sample
16
     *
17
     * @return mixed
18
     */
19
    private function predictSample(array $sample)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
20
    {
21
        $predictions = [];
22
        foreach ($this->labels as $index => $label) {
23
            $predictions[$label] = 0;
24
            foreach ($sample as $token => $count) {
25
                if (array_key_exists($token, $this->samples[$index])) {
26
                    $predictions[$label] += $count * $this->samples[$index][$token];
27
                }
28
            }
29
        }
30
31
        arsort($predictions, SORT_NUMERIC);
32
        reset($predictions);
33
34
        return key($predictions);
35
    }
36
}
37