Passed
Push — master ( 4b5d57...3ba359 )
by Arkadiusz
02:42
created

VarianceThreshold   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A fit() 0 12 3
A transform() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\FeatureSelection;
6
7
use Phpml\Exception\InvalidArgumentException;
8
use Phpml\Math\Matrix;
9
use Phpml\Math\Statistic\Variance;
10
use Phpml\Transformer;
11
12
final class VarianceThreshold implements Transformer
13
{
14
    /**
15
     * @var float
16
     */
17
    private $threshold;
18
19
    /**
20
     * @var array
21
     */
22
    private $variances = [];
23
24
    /**
25
     * @var array
26
     */
27
    private $keepColumns = [];
28
29
    public function __construct(float $threshold = 0.0)
30
    {
31
        if ($threshold < 0) {
32
            throw new InvalidArgumentException('Threshold can\'t be lower than zero');
33
        }
34
35
        $this->threshold = $threshold;
36
        $this->variances = [];
37
        $this->keepColumns = [];
38
    }
39
40
    public function fit(array $samples): void
41
    {
42
        $this->variances = array_map(function (array $column) {
43
            return Variance::population($column);
44
        }, Matrix::transposeArray($samples));
45
46
        foreach ($this->variances as $column => $variance) {
47
            if ($variance > $this->threshold) {
48
                $this->keepColumns[$column] = true;
49
            }
50
        }
51
    }
52
53
    public function transform(array &$samples): void
54
    {
55
        foreach ($samples as &$sample) {
56
            $sample = array_values(array_intersect_key($sample, $this->keepColumns));
57
        }
58
    }
59
}
60