0

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

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.

4
  • Double check the shapes of those 300 arrays. Getting a 1d array when you expect 4 is caused by a mix of shapes. What's the 1d array dtype? object is another indicator of ragged inputs. The newest version, 1.19, is now give a warning if this happens. Commented Aug 2, 2020 at 16:44
  • @hpaulj you're right there was the following warning: Commented Aug 2, 2020 at 16:48
  • @hpaulj 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 return array(a, dtype, copy=False, order=order) so i turned my conversion into this np.asarray(my_list, dtype=object) and the warning disappeared but the problem persists. Commented Aug 2, 2020 at 16:49
  • You still have a ragged array! Check the shape of all your 300 arrays. Commented Aug 2, 2020 at 16:53

3 Answers 3

1

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

If shapes all match:

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)

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)
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example:

data1=np.random.random(size=(280, 190, 3))
data2=np.random.random(size=(280, 190, 3))
data1.shape, data2.shape
((280, 190, 3), (280, 190, 3))
np.stack([data1,data2], axis=0).shape
(2, 280, 190, 3)

4 Comments

but you aren't trying to create a 'ragged' array. The OP is - albeit unintentionally.
@hpaulj are you calling it a ragged array because all my arrays' shapes are the same? What is the difference between an array of arrays and a ragged array?
In my experience if I get the 1.19 ragged warning, I also get an error message from np.stack about shapes that don't match.
@hpaulj yep but when I did it her way the error made me understand what the problem was! Sorry I didn't even know what a ragged array is..
0

Use the numpy stack to stack arrays separately. Documentation: https://numpy.org/doc/stable/reference/generated/numpy.stack.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.