numpy“模块"对象没有属性“堆栈" [英] numpy 'module' object has no attribute 'stack'

查看:146
本文介绍了numpy“模块"对象没有属性“堆栈"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一些代码(不是我的),在numpy库中使用了堆栈".

I am trying to run some code (which is not mine), where is used 'stack' from numpy library.

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

Looking into documentation, stack really exists in numpy: https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.stack.html

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

but when I run the code, I got:

AttributeError: 'module' object has no attribute 'stack'

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

any idea how to fix this. code extract:

s_t = np.stack((x_t, x_t, x_t, x_t), axis = 2)

我需要一些旧图书馆吗?

do I need some old libraries?

谢谢.

由于某种原因,python使用了较旧版本的numpy库. pip2冻结打印"numpy == 1.10.4".我还重新安装了numpy,并获得了成功安装了numpy-1.10.4"的信息,但是在代码中打印np.version.version给了我1.8.2.

for some reason, python uses older version of numpy library. pip2 freeze prints "numpy==1.10.4". I've also reinstalled numpy and I've got "Successfully installed numpy-1.10.4", but printing np.version.version in code gives me 1.8.2.

推荐答案

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

The function numpy.stack is new; it appeared in numpy == 1.10.0. If you can't get that version running on your system, the code can be found at (near the end)

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

我需要再检查一点,但是该功能的工作部分是:

I need to examine it a bit more, but the working part of the function is:

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

因此它将np.newaxis添加到每个数组,然后在该数组上进行串联.像vstackhstackdstack一样,它会调整输入的尺寸,然后使用np.concatenate.没什么特别新颖的或不可思议的.

So it adds a np.newaxis to each array, and then concatenate on that. So like, vstack, hstack and dstack it adjusts the dimensions of the inputs, and then uses np.concatenate. Nothing particularly new or magical.

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

So if x is (2,3) shape, x[:,np.newaxis] is (2,1,3), x[:,:,np.newaxis] is (2,3,1) etc.

如果x_t是2d,则

np.stack((x_t, x_t, x_t, x_t), axis = 2)

可能等同于

np.dstack((x_t, x_t, x_t, x_t))

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

creating a new array that has size 4 on axis 2.

或者:

tmp = x_t[:,:,None]
np.concatenate((tmp,tmp,tmp,tmp), axis=2)

这篇关于numpy“模块"对象没有属性“堆栈"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆