-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathKNNImputer.php
260 lines (222 loc) · 7.34 KB
/
KNNImputer.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace Rubix\ML\Transformers;
use Rubix\ML\DataType;
use Rubix\ML\Persistable;
use Rubix\ML\Helpers\Stats;
use Rubix\ML\Datasets\Dataset;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Graph\Trees\Spatial;
use Rubix\ML\Graph\Trees\BallTree;
use Rubix\ML\Kernels\Distance\NaNSafe;
use Rubix\ML\Traits\AutotrackRevisions;
use Rubix\ML\Kernels\Distance\Distance;
use Rubix\ML\Kernels\Distance\SafeEuclidean;
use Rubix\ML\Specifications\SamplesAreCompatibleWithTransformer;
use Rubix\ML\Exceptions\InvalidArgumentException;
use Rubix\ML\Exceptions\RuntimeException;
use function Rubix\ML\argmax;
use function is_float;
use function is_nan;
use function in_array;
use function array_column;
use function array_count_values;
use function array_fill_keys;
use function array_unique;
/**
* KNN Imputer
*
* An unsupervised imputer that replaces missing values in datasets with the distance-weighted
* average of the samples' *k* nearest neighbors' values. The average for a continuous feature
* column is defined as the mean of the values of each donor sample while average is defined as
* the most frequent for categorical features.
*
* **Note:** Requires NaN-safe distance kernels, such as Safe Euclidean, for continuous features.
*
* References:
* [1] O. Troyanskaya et al. (2001). Missing value estimation methods for DNA microarrays.
*
* @category Machine Learning
* @package Rubix/ML
* @author Andrew DalPino
*/
class KNNImputer implements Transformer, Stateful, Persistable
{
use AutotrackRevisions;
/**
* The number of donor samples to consider when imputing a value.
*
* @var int
*/
protected int $k;
/**
* Should the imputed values be sampled from a distribution weighted by distance?
*
* @var bool
*/
protected bool $weighted;
/**
* The placeholder category that denotes missing values.
*
* @var string
*/
protected string $categoricalPlaceholder;
/**
* The spatial tree used to run nearest neighbor searches.
*
* @var Spatial
*/
protected Spatial $tree;
/**
* The data types of the fitted feature columns.
*
* @var DataType[]|null
*/
protected ?array $types = null;
/**
* @param int $k
* @param bool $weighted
* @param string $categoricalPlaceholder
* @param Spatial|null $tree
* @throws InvalidArgumentException
*/
public function __construct(
int $k = 5,
bool $weighted = false,
string $categoricalPlaceholder = '?',
?Spatial $tree = null
) {
if ($k < 1) {
throw new InvalidArgumentException('At least 1 donor is required'
. " to impute a value, $k given.");
}
if ($tree and in_array(DataType::continuous(), $tree->kernel()->compatibility())) {
$kernel = $tree->kernel();
if (!$kernel instanceof NaNSafe) {
throw new InvalidArgumentException('Continuous distance kernels'
. ' must implement the NaNSafe interface.');
}
}
if (empty($categoricalPlaceholder)) {
throw new InvalidArgumentException('Categorical placeholder cannot be empty.');
}
$this->k = $k;
$this->weighted = $weighted;
$this->categoricalPlaceholder = $categoricalPlaceholder;
$this->tree = $tree ?? new BallTree(30, new SafeEuclidean());
}
/**
* Return the data types that this transformer is compatible with.
*
* @internal
*
* @return list<DataType>
*/
public function compatibility() : array
{
return $this->tree->kernel()->compatibility();
}
/**
* Is the transformer fitted?
*
* @return bool
*/
public function fitted() : bool
{
return !$this->tree->bare();
}
/**
* Fit the transformer to a dataset.
*
* @param Dataset $dataset
* @throws RuntimeException
*/
public function fit(Dataset $dataset) : void
{
SamplesAreCompatibleWithTransformer::with($dataset, $this)->check();
$donors = [];
foreach ($dataset->samples() as $sample) {
foreach ($sample as $value) {
if (is_float($value)) {
if (is_nan($value)) {
continue 2;
}
} else {
if ($value === $this->categoricalPlaceholder) {
continue 2;
}
}
}
$donors[] = $sample;
}
if (empty($donors)) {
throw new RuntimeException('No complete donors found in dataset.');
}
$labels = array_fill(0, count($donors), '');
$this->tree->grow(Labeled::quick($donors, $labels));
$this->types = $dataset->featureTypes();
}
/**
* Transform the dataset in place.
*
* @param list<list<mixed>> $samples
* @throws RuntimeException
*/
public function transform(array &$samples) : void
{
if ($this->tree->bare() or $this->types === null) {
throw new RuntimeException('Transformer has not been fitted.');
}
foreach ($samples as &$sample) {
$donors = [];
foreach ($sample as $column => &$value) {
if (is_float($value) && is_nan($value) or $value === $this->categoricalPlaceholder) {
if (empty($donors)) {
[$donors, $labels, $distances] = $this->tree->nearest($sample, $this->k);
if ($this->weighted) {
$weights = [];
foreach ($distances as $distance) {
$weights[] = 1.0 / (1.0 + $distance);
}
}
}
$values = array_column($donors, $column);
$type = $this->types[$column];
switch ($type) {
case DataType::continuous():
if (isset($weights)) {
$value = Stats::weightedMean($values, $weights);
} else {
$value = Stats::mean($values);
}
break;
case DataType::categorical():
default:
if (isset($weights)) {
$scores = array_fill_keys(array_unique($values), 0.0);
foreach ($weights as $i => $weight) {
$scores[$values[$i]] += $weight;
}
} else {
$scores = array_count_values($values);
}
$value = argmax($scores);
break;
}
}
}
}
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return "KNN Imputer (k: {$this->k}, weighted: {$this->weighted},"
. " categorical placeholder: {$this->categoricalPlaceholder},"
. " tree: {$this->tree})";
}
}