Find the `k` nearest neighbors in the ball tree to a query vector `x` using the KNS1 algorithm. Parameters ---------- k : int The number of closest points in `X` to return x : :py:class:`ndarray ` of shape `(1, M)`
(self, k, x)
| 290 | return centroid, left_X, left_y, right_X, right_y |
| 291 | |
| 292 | def nearest_neighbors(self, k, x): |
| 293 | """ |
| 294 | Find the `k` nearest neighbors in the ball tree to a query vector `x` |
| 295 | using the KNS1 algorithm. |
| 296 | |
| 297 | Parameters |
| 298 | ---------- |
| 299 | k : int |
| 300 | The number of closest points in `X` to return |
| 301 | x : :py:class:`ndarray <numpy.ndarray>` of shape `(1, M)` |
| 302 | The query vector. |
| 303 | |
| 304 | Returns |
| 305 | ------- |
| 306 | nearest : list of :class:`PQNode` s of length `k` |
| 307 | List of the `k` points in `X` to closest to the query vector. The |
| 308 | ``key`` attribute of each :class:`PQNode` contains the point itself, the |
| 309 | ``val`` attribute contains its target, and the ``distance`` |
| 310 | attribute contains its distance to the query vector. |
| 311 | """ |
| 312 | # maintain a max-first priority queue with priority = distance to x |
| 313 | PQ = PriorityQueue(capacity=k, heap_order="max") |
| 314 | nearest = self._knn(k, x, PQ, self.root) |
| 315 | for n in nearest: |
| 316 | n.distance = self.metric(x, n.key) |
| 317 | return nearest |
| 318 | |
| 319 | def _knn(self, k, x, PQ, root): |
| 320 | dist = self.metric |