如何从 sklearn.neighbors.KDTree 中检索节点?
How to retrieve nodes from a sklearn.neighbors.KDTree?
有没有办法从 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')])
有没有办法从 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')])