NumPy数组形状错误 [英] NumPy array mis-shaped dimension

查看:219
本文介绍了NumPy数组形状错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是numpy的新手,正试图了解多维的工作原理。

I am new to numpy and trying to understand how multi-dimensions work.

我有300个numpy维度数组(280、190、3)。当我将所有这些数组追加到列表中并将其转换为numpy数组(我认为这是我做错事情的地方)时,我希望其形状为(300,280,190,3),但我得到的只是(300 ,),就好像它是一维数组。

I have 300 numpy arrays of dimension (280, 190, 3). When I append all these arrays into a list and convert it into a numpy array (I think it's here where I am doing something wrong) I expect its shape to be (300, 280, 190, 3) but all I get is (300, ) as if it is a 1D array.

您能告诉我我的错误步骤是什么吗?欢迎任何其他信息。谢谢。

Could you please tell me what is my wrong step? Any additional information welcome. Thanks.

推荐答案

要说明一维衣衫warning的警告情况:

To illustrate the 1d ragged warning case:

In [461]: alist = [np.ones((10,5,3)), np.ones((10,5,3)), np.ones((9,5,3))]                           

In [463]: np.array(alist).shape                                                                      
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  #!/usr/bin/python3
Out[463]: (3,)

堆栈在相同情况下会产生错误:

stack in the same case produces an error:

In [464]: np.stack(alist)                                                                            
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-464-724d9c1d0554> in <module>
----> 1 np.stack(alist)

<__array_function__ internals> in stack(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
    425     shapes = {arr.shape for arr in arrays}
    426     if len(shapes) != 1:
--> 427         raise ValueError('all input arrays must have the same shape')
    428 
    429     result_ndim = arrays[0].ndim + 1

ValueError: all input arrays must have the same shape

如果形状都匹配:

In [465]: alist = [np.ones((10,5,3)), np.ones((10,5,3)), np.ones((10,5,3))]                          
In [466]: np.array(alist).shape                                                                      
Out[466]: (3, 10, 5, 3)
In [467]: np.stack(alist).shape                                                                      
Out[467]: (3, 10, 5, 3)

制作1d对象从相等大小的数组列表中删除数组,我们必须做一些额外的工作:

To make a 1d object array from a list of equal size arrays, we have to do some extra work:

In [468]: arr = np.empty(3,object)                                                                   
In [469]: arr[:] = alist                                                                             
In [470]: arr.shape                                                                                  
Out[470]: (3,)
In [471]: np.array(arr).shape            # np.array doesn't change this                                                                
Out[471]: (3,)
In [472]: np.stack(arr).shape            # stack still works                                                            
Out[472]: (3, 10, 5, 3)

这篇关于NumPy数组形状错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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