Completed
Push — develop ( cc50d2...26f2cb )
by Arkadiusz
02:52
created

Normalizer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 75
wmc 12
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A normalizeL1() 0 16 4
A normalizeL2() 0 16 4
A transform() 0 7 2
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Preprocessing;
6
7
use Phpml\Exception\NormalizerException;
8
9
class Normalizer implements Preprocessor
10
{
11
    const NORM_L1 = 1;
12
    const NORM_L2 = 2;
13
14
    /**
15
     * @var int
16
     */
17
    private $norm;
18
19
    /**
20
     * @param int $norm
21
     *
22
     * @throws NormalizerException
23
     */
24
    public function __construct(int $norm = self::NORM_L2)
25
    {
26
        if (!in_array($norm, [self::NORM_L1, self::NORM_L2])) {
27
            throw NormalizerException::unknownNorm();
28
        }
29
30
        $this->norm = $norm;
31
    }
32
33
    /**
34
     * @param array $samples
35
     */
36
    public function transform(array &$samples)
37
    {
38
        $method = sprintf('normalizeL%s', $this->norm);
39
        foreach ($samples as &$sample) {
40
            $this->$method($sample);
41
        }
42
    }
43
44
    /**
45
     * @param array $sample
46
     */
47
    private function normalizeL1(array &$sample)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        $norm1 = 0;
50
        foreach ($sample as $feature) {
51
            $norm1 += abs($feature);
52
        }
53
54
        if (0 == $norm1) {
55
            $count = count($sample);
56
            $sample = array_fill(0, $count, 1.0 / $count);
57
        } else {
58
            foreach ($sample as &$feature) {
59
                $feature = $feature / $norm1;
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param array $sample
66
     */
67
    private function normalizeL2(array &$sample)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $norm2 = 0;
70
        foreach ($sample as $feature) {
71
            $norm2 += $feature * $feature;
72
        }
73
        $norm2 = sqrt($norm2);
74
75
        if (0 == $norm2) {
76
            $sample = array_fill(0, count($sample), 1);
77
        } else {
78
            foreach ($sample as &$feature) {
79
                $feature = $feature / $norm2;
80
            }
81
        }
82
    }
83
}
84