0

I want to zip two arrays that have the same lengths but contain different dimensions of matrices.

a1 = np.ones((2,2,3),dtype=np.uint8)
a2 = np.ones((2,2,3),dtype=np.uint8)*2
b1 = np.ones((2,2),dtype=np.uint8)
b2 = np.ones((2,2),dtype=np.uint8)*2

Now Two arrays are c1, c2.

c1 = [a1,a2]
c2 = [b1,b2]

when I try to zip them like this.

res = np.array(list(zip(c1,c2)))

I get an error like following.

Traceback (most recent call last): File "", line 1, in ValueError: could not broadcast input array from shape (2,2,3) into shape (2,2)

Why I am getting this error? How Can I fix it?

5
  • You are getting this error because a2 is 3D array and a1,b1,a2 are 2D array so shape of array a2 is different Commented Mar 24, 2021 at 9:26
  • @AnuragDabas How can I fix it? I actually want to randomize the array afterward. Commented Mar 24, 2021 at 9:34
  • Could you give us your expected output? res = ? Commented Mar 24, 2021 at 10:14
  • @RikardOlsson for this my expected output would be [(a1,b1),(a2,b2)]. Commented Mar 24, 2021 at 10:24
  • As Anurag says, you cannot create an array of arrays that have different shapes (try your self to describe the shape of res). If you try and explain in more detail what your expected output is and what you want to do with it, it will be easier to help you Commented Mar 24, 2021 at 10:59

1 Answer 1

1

c1 and c2 are lists, not arrays. Combining them produces a 2 element list of tuples:

In [84]: C = list(zip(c1,c2))
In [85]: C
Out[85]: 
[(array([[[1, 1, 1],
          [1, 1, 1]],
  
         [[1, 1, 1],
          [1, 1, 1]]], dtype=uint8),
  array([[1, 1],
         [1, 1]], dtype=uint8)),
 (array([[[2, 2, 2],
          [2, 2, 2]],
  
         [[2, 2, 2],
          [2, 2, 2]]], dtype=uint8),
  array([[2, 2],
         [2, 2]], dtype=uint8))]

This is a mix of (2,2,3) and (2,2) shaped arrays. Sometimes when trying to make an array from mixed shape arrays, we get a ragged array warning (as of numpy v1.19) and an object dtype array. But with this combination of shapes, the result is an error.

A way around that error is to create an object dtype array with the desire shape, and fill it from the list:

In [93]: arr = np.empty((2,2), object)
In [94]: arr[:]=C
In [95]: arr
Out[95]: 
array([[array([[[1, 1, 1],
                [1, 1, 1]],

               [[1, 1, 1],
                [1, 1, 1]]], dtype=uint8), array([[1, 1],
                                                  [1, 1]], dtype=uint8)],
       [array([[[2, 2, 2],
                [2, 2, 2]],

               [[2, 2, 2],
                [2, 2, 2]]], dtype=uint8), array([[2, 2],
                                                  [2, 2]], dtype=uint8)]],
      dtype=object)

Apart from the print display, the array has, few if any, advantages over the C list.

Sign up to request clarification or add additional context in comments.

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.