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

Accuracy::score()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
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 19
rs 8.8571
cc 5
eloc 10
nc 7
nop 3
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