sklearn.neighbors.NearestNeighbors上有n个点的中点函数吗?

Is there a function for finding the midpoint of n points on sklearn.neighbors.NearestNeighbors?

我有一个受过训练的 Nearest Neighbor model

radius_neighbors() returns n 个点(例如 5)的此半径内的所有索引。 然而,我想要得到的是这 n 个(例如 5)个点的中点(即中心)。 sklearn 中有没有可以做到这一点的方法?或者任何低复杂度的方法?

谢谢。

函数的第二个 return 值为您提供样本中所需点的索引。基本上 midpoint 是通过取每列的平均值形成的。您可以使用 np.array 对象的 mean 方法来做到这一点。假设原始点集是 samples,并且所需点的索引是 inds:

import numpy as np
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
inds = [1,2]
print(np.array(samples)[inds].mean(axis=0))

在实践中 inds 将由 radius_neighbors 在您训练的模型上给出:

inds = yournnmodel.radius_neighbors(your_query_point).asarray()[1][0]