运行SciPy KDTree示例时出错

时间:2015-02-16 22:12:46

标签: scipy python-3.4 kdtree

使用Scipy on Python 3.4,当我运行here的最小KDTree示例时:

from scipy import spatial
x, y = np.mgrid[0:5, 2:8]
tree = spatial.KDTree(zip(x.ravel(), y.ravel()))

我收到此错误:

File "C:/_work/kdtree.py", line 9, in <module>
tree = spatial.KDTree(zip(x.ravel(), y.ravel()))
File "C:\Python34\lib\site-packages\scipy\spatial\kdtree.py", line 229, in __init__
self.n, self.m = np.shape(self.data)
ValueError: need more than 0 values to unpack

我做错了什么?我尝试过使用scipy 14.0和15.1

1 个答案:

答案 0 :(得分:2)

这是docstring中的一个错误。 KDTree的参数必须是&#34; array_like&#34;,但在Python 3中,zip返回的对象不是&#34; array_like&#34;。您可以将示例更改为

tree = spatial.KDTree(list(zip(x.ravel(), y.ravel())))

或者,您可以使用zip

,而不是使用KDTree来创建np.column_stack的输入。
x, y = np.mgrid[0:5, 2:8]
xy = np.column_stack((x.ravel(), y.ravel()))
tree = spatial.KDTree(xy)

无论是哪种更改,示例的其余部分都应该有效。