Passed
Push — develop ( 63d496...62ec4e )
by Arkadiusz
02:18
created

Accuracy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B score() 0 19 5
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Metric;
6
7
use Phpml\Exception\InvalidArgumentException;
8
9
class Accuracy
10
{
11
    /**
12
     * @param array $actualLabels
13
     * @param array $predictedLabels
14
     * @param bool  $normalize
15
     *
16
     * @return float|int
17
     *
18
     * @throws InvalidArgumentException
19
     */
20
    public static function score(array $actualLabels, array $predictedLabels, bool $normalize = true)
21
    {
22
        if (count($actualLabels) != count($predictedLabels)) {
23
            throw InvalidArgumentException::sizeNotMatch();
24
        }
25
26
        $score = 0;
27
        foreach ($actualLabels as $index => $label) {
28
            if ($label === $predictedLabels[$index]) {
29
                ++$score;
30
            }
31
        }
32
33
        if ($normalize) {
34
            $score = $score / count($actualLabels);
35
        }
36
37
        return $score;
38
    }
39
}
40