Total Complexity | 6 |
Total Lines | 62 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
12 | class KNearestNeighbors implements Classifier |
||
13 | { |
||
14 | use Trainable; |
||
15 | use Predictable; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $k; |
||
21 | |||
22 | /** |
||
23 | * @var Distance |
||
24 | */ |
||
25 | private $distanceMetric; |
||
26 | |||
27 | /** |
||
28 | * @param Distance|null $distanceMetric (if null then Euclidean distance as default) |
||
29 | */ |
||
30 | public function __construct(int $k = 3, ?Distance $distanceMetric = null) |
||
31 | { |
||
32 | if ($distanceMetric === null) { |
||
33 | $distanceMetric = new Euclidean(); |
||
34 | } |
||
35 | |||
36 | $this->k = $k; |
||
37 | $this->samples = []; |
||
38 | $this->targets = []; |
||
39 | $this->distanceMetric = $distanceMetric; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return mixed |
||
44 | */ |
||
45 | protected function predictSample(array $sample) |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @throws \Phpml\Exception\InvalidArgumentException |
||
62 | */ |
||
63 | private function kNeighborsDistances(array $sample): array |
||
74 | } |
||
75 | } |
||
76 |