numpy 函数 np.linspace 抛出值错误



我之前在不同的机器上运行过相同的代码,它工作得很好。

def interpolate2D(a, b, c, d, n=10):
assert(len(a) == len(b) == len(c) == len(d))
dim = len(a)
# Define the interpolated values between the vectors a to b and the vectors c to d
print(type(a), type(b), type(c), type(d))
x = np.linspace(a, b, num=n)
y = np.linspace(c, d, num=n)
...

现在,对np.linspace的第一次调用会引发以下错误。这是版本控制问题吗?我不知道还能是什么。

Traceback (most recent call last):
File "/home/alex/Documents/all_is_all_poetry/in_two_dimensions/grid.py", line 268, in <module>
'thing',
File "/home/alex/Documents/all_is_all_poetry/in_two_dimensions/grid.py", line 242, in plot_along_space_2d
vector_grid = interpolate2D(w, v, x, y, n=n)
File "/home/alex/Documents/all_is_all_poetry/in_two_dimensions/grid.py", line 91, in interpolate2D
x = np.linspace(a, b, num=n)
File "/home/alex/anaconda3/envs/nlp/lib/python3.7/site-packages/numpy/core/function_base.py", line 124, in linspace
if step == 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我的数字是最新的:

>>> numpy.version.version
'1.16.4'

似乎您的 anaconda 环境不在 1.16.4 上,而是在 1.15.x 上。

if step == 0在 1.16.0 版中更改为if _nx.any(step == 0):,以便开始和停止可以是数组。

在此处更改日志:https://github.com/numpy/numpy/commit/58ebb6a7d77cf89afeb888a70aff23e03d213788#diff-12e00d917c1600a79611d57403cfbf70

您可以在此处看到 1.15.4,第 124 行与上面的错误消息匹配 https://github.com/numpy/numpy/blob/maintenance/1.15.x/numpy/core/function_base.py

最新更新