gpt4 book ai didi

python - 如何从 sklearn.neighbors.KDTree 检索节点?

转载 作者:行者123 更新时间:2023-12-01 07:13:10 25 4
gpt4 key购买 nike

有没有办法从 sklearn.neighbors.KDTree 实例中通过 id 获取节点或所有节点?

  from sklearn.neighbors import KDTree
import numpy as np

tree = KDTree(np.array([[0., 0., 0.], [1., 1., 1.]]))
# How to get the array [[0., 0., 0.], [1., 1., 1.]] back out?
# Or something like this:
tree.get_node(0)
# Which would return: [0., 0., 0.]

最佳答案

.get_arrays() 方法使您可以访问所有数组:

from sklearn.neighbors import KDTree
import numpy as np

X = np.array([[-2, -2], [-1, -1], [-2, -1], [-3, -2], [0, 0],
[1, 1], [2, 1], [2, 2], [3, 2]])
kdt = KDTree(X, leaf_size=3, metric='euclidean')
tree_data, index, tree_nodes, node_bounds = kdt.get_arrays()
tree_nodes

输出

array([(0, 9, 0, 3.60555128), (0, 4, 1, 1.11803399),
(4, 9, 1, 1.80277564)],
dtype=[('idx_start', '<i8'), ('idx_end', '<i8'), ('is_leaf', '<i8'), ('radius', '<f8')])

关于python - 如何从 sklearn.neighbors.KDTree 检索节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58100849/

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