Completed
Push — develop ( 633974...f7b91b )
by Arkadiusz
02:36
created

NaiveBayes   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
wmc 4
lcom 1
cbo 2
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A predictSample() 0 17 4
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Classification;
6
7
use Phpml\Classification\Traits\Predictable;
8
use Phpml\Classification\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
    protected function predictSample(array $sample)
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