1 | <?php |
||
9 | class KNearestNeighbors implements Classifier |
||
10 | { |
||
11 | /** |
||
12 | * @var int |
||
13 | */ |
||
14 | private $k; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $samples; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $labels; |
||
25 | |||
26 | /** |
||
27 | * @param int $k |
||
28 | */ |
||
29 | public function __construct(int $k = 3) |
||
35 | |||
36 | /** |
||
37 | * @param array $samples |
||
38 | * @param array $labels |
||
39 | */ |
||
40 | public function train(array $samples, array $labels) |
||
45 | |||
46 | /** |
||
47 | * @param array $samples |
||
48 | * |
||
49 | * @return mixed |
||
50 | */ |
||
51 | public function predict(array $samples) |
||
64 | |||
65 | /** |
||
66 | * @param array $sample |
||
67 | * |
||
68 | * @return mixed |
||
69 | */ |
||
70 | private function predictSample(array $sample) |
||
85 | |||
86 | /** |
||
87 | * @param array $sample |
||
88 | * |
||
89 | * @return array |
||
90 | * |
||
91 | * @throws \Phpml\Exception\InvalidArgumentException |
||
92 | */ |
||
93 | private function kNeighborsDistances(array $sample): array |
||
105 | } |
||
106 |