我有一个带有scipy的cKDTree的toyexample,效果很好,我想用scikit-learn的BallTree做类似的代码
import numpy as np
from scipy import spatial
min_neighbors = 3
x,y = np.mgrid[0:5,0:5]
grid_x,grid_y = np.mgrid[1:6,1:6]
points = np.c_[x.ravel(),y.ravel()]
grid_points = np.c_[grid_x.ravel(),grid_y.ravel()]
tree = spatial.cKDTree(points)
indices = tree.query_ball_point(grid_points,r=1)
for idx,(matches,grid) in enumerate(zip(indices,grid_points)):
if len(matches) >= min_neighbors:
x1,y1 = tree.data[matches].T
当我用BallTree做类似的玩具示例时,如下所示
import numpy as np
from sklearn.neighbors.ball_tree import BallTree
from sklearn.neighbors import NearestNeighbors
import sys
def main():
min_neighbors = 3
x,y = np.mgrid[0:5,0:5]
grid_x,grid_y = np.mgrid[1:6,1:6]
points = np.c_[x.ravel(),y.ravel()]
grid_points = np.c_[grid_x.ravel(),grid_y.ravel()]
bt = BallTree(points,leaf_size=1, metric='haversine')
indices = bt.query_radius(grid_points,1)
for idx,(matches,grid) in enumerate(zip(indices,grid_points)):
#print(matches)
if len(matches) >= min_neighbors:
x1,y1 = bt.data[matches].T
main()
我收到以下错误-
Traceback (most recent call last):
File "testballtree.py", line 25, in <module>
main()
File "testballtree.py", line 23, in main
x1,y1 = bt.data[matches].T
File "stringsource", line 406, in View.MemoryView.memoryview.__getitem__
File "stringsource", line 746, in View.MemoryView.memview_slice
TypeError: only integer scalar arrays can be converted to a scalar index
以与scipy相同的方式访问scikit-learn的BallTree中的data属性的确切方法是什么?
scikit-learn version is 0.19.2
答案 0 :(得分:2)
看起来文档是错误的,bt.data
是内存视图,而不是numpy数组。它可能应该是一个numpy数组或私有的。不过,您可以改为使用points
来修复代码段。打开https://github.com/scikit-learn/scikit-learn/issues/11728
答案 1 :(得分:1)
BallTree.data
是a view or a copy of the training data,因此在上面的示例中,您可以直接使用points
数组。
data
属性文档字符串确实不正确:它是一个memoryview而不是一个数组。您可以使用numpy.asarray(bt.data)
将其转换回numpy数组。