-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathVarianceThresholdFilter.php
More file actions
78 lines (68 loc) · 1.6 KB
/
Copy pathVarianceThresholdFilter.php
File metadata and controls
78 lines (68 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Rubix\Engine\Transformers;
use Rubix\Engine\Dataset;
use MathPHP\Statistics\Descriptive;
class VarianceThresholdFilter implements Transformer
{
/**
* The minimum variance a feature column must have in order to be selected.
*
* @var float
*/
protected $threshold;
/**
* The feature columns that have been selected.
*
* @var array
*/
protected $selected = [
//
];
/**
* @param float $threshold
* @return void
*/
public function __construct(float $threshold = 0.0)
{
if ($threshold < 0.0) {
throw new InvalidArgumentException('Threshold must be a float value greater than 0.');
}
$this->threshold = $threshold;
}
/**
* @return float
*/
public function threshold() : float
{
return $this->threshold;
}
/**
* @return array
*/
public function selected() : array
{
return array_keys($this->selected);
}
/**
* @param \Rubix\Engine\Dataset $data
* @return void
*/
public function fit(Dataset $data) : void
{
foreach ($data->rotate() as $column => $data) {
if (Descriptive::populationVariance($data) > $this->threshold) {
$this->selected[$column] = true;
}
}
}
/**
* @param array $samples
* @return array
*/
public function transform(array &$samples) : void
{
foreach ($samples as &$sample) {
$sample = array_values(array_intersect_key($sample, $this->selected));
}
}
}