gpt4 book ai didi

python - Scikit-learn KDTree query_radius 同时返回 count 和 ind?

转载 作者:行者123 更新时间:2023-11-30 09:17:16 25 4
gpt4 key购买 nike

我正在尝试同时返回 count (邻居数量)和 ind (所述邻居的索引)但我不能,除非我打电话 query_radius两次,虽然计算量很大,但对我来说,在 Python 中实际上比迭代并计算 ind 中每行的大小更快 !这看起来效率非常低,所以我想知道有没有一种方法可以在一次调用中同时返回它们?

我尝试访问 tree 的 count 和 ind 对象调用query_radius后但它不存在。在 numpy 中没有有效的方法来做到这一点,不是吗?

>>> array = np.array([[1,2,3], [2,3,4], [6,2,3]])
>>> tree = KDTree(array)
>>> neighbors = tree.query_radius(array, 1)
>>> tree.ind
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'sklearn.neighbors.kd_tree.KDTree' object has no attribute 'ind'
>>> tree.count
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'sklearn.neighbors.kd_tree.KDTree' object has no attribute 'count'

最佳答案

不知道为什么你认为你需要这样做两次:

a = np.random.rand(100,3)*10
tree = KDTree(a)
neighbors = tree.query_radius(a, 1)

%timeit counts = tree.query_radius(a, 1, count_only = 1)
1000 loops, best of 3: 231 µs per loop

%timeit counts = np.array([arr.size for arr in neighbors])
The slowest run took 5.66 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 22.5 µs per loop

仅查找neighbors中数组对象的大小比重做tree.query_radius要快得多

关于python - Scikit-learn KDTree query_radius 同时返回 count 和 ind?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52324800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com