numpy'module'对象没有属性'stack'

Snu*_*ill 6 python numpy

我试图运行一些代码(这不是我的),其中使用numpy库中的'stack'.

查看文档,堆栈确实存在于numpy中:https: //docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.stack.html

但是当我运行代码时,我得到了:

AttributeError: 'module' object has no attribute 'stack'
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决这个问题.代码提取:

s_t = np.stack((x_t, x_t, x_t, x_t), axis = 2)
Run Code Online (Sandbox Code Playgroud)

我需要一些旧库吗?

谢谢.

编辑:由于某种原因,python使用旧版本的numpy库.pip2冻结打印"numpy == 1.10.4".我也重新安装了numpy,我已经"成功安装了numpy-1.10.4",但在代码中打印np.version.version给了我1.8.2.

hpa*_*ulj 5

该功能numpy.stack是新的;它出现在numpy == 1.10.0。如果您无法在系统上运行该版本,则可以在(接近尾声)找到代码。

https://github.com/numpy/numpy/blob/f4cc58c80df5202a743bddd514a3485d5e4ec5a4/numpy/core/shape_base.py

我需要对其进行更多检查,但是该功能的工作部分是:

sl = (slice(None),) * axis + (_nx.newaxis,)
expanded_arrays = [arr[sl] for arr in arrays]
return _nx.concatenate(expanded_arrays, axis=axis)
Run Code Online (Sandbox Code Playgroud)

因此,它np.newaxis向每个数组添加一个,然后在该数组上进行串联。所以像,vstackhstackdstack它调整的输入尺寸,然后使用np.concatenate。没有什么特别新颖或神奇的。

因此,如果x(2,3)形状,x[:,np.newaxis](2,1,3)x[:,:,np.newaxis](2,3,1)等。

如果x_t是2d,则

np.stack((x_t, x_t, x_t, x_t), axis = 2)
Run Code Online (Sandbox Code Playgroud)

大概等于

np.dstack((x_t, x_t, x_t, x_t))
Run Code Online (Sandbox Code Playgroud)

创建一个在轴2上大小为4的新数组。

要么:

tmp = x_t[:,:,None]
np.concatenate((tmp,tmp,tmp,tmp), axis=2)
Run Code Online (Sandbox Code Playgroud)