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

NaiveBayes   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 4
c 9
b 0
f 1
lcom 1
cbo 2
dl 0
loc 27
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\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