Home > Net >  Numpy docs: How to multiply 2 arrays of different sizes together?
Numpy docs: How to multiply 2 arrays of different sizes together?

Time:08-26

Numpy docs claims you can multiply arrays of different lengths together, however it is not working. I'm definitely misinterpreting what its saying but there's no example to go with their text. From enter image description here

Therefore, I created some code to try it out but I'm getting an error that says ValueError: operands could not be broadcast together with shapes (4,1) (3,1). Same error if I try this with shapes (4,) and (3,).

a = np.array([[1.0],
             [1.0],
             [1.0],
             [1.0]])
print(a.shape)
b = np.array([[2.0],
             [2.0],
             [2.0]])
print(b.shape)
a*b

CodePudding user response:

You can multiply arrays together if every dimenssion has the same length or one of the arrays has dimension 1 in the current axis. in your example the arrays has sizes 4x1 and 3x1. So if you want to multiply them together you need to transpose one:

a = np.array([[1.0],
             [1.0],
             [1.0],
             [1.0]])
print(a.shape)
b = np.array([[2.0],
             [2.0],
             [2.0]])
print(b.shape)
a*b.T

So its dimensions are shared with 1 in the other array 4x1 and 1x3 now and the result will have size 4x3

CodePudding user response:

Copying and pasting the immediately previous text, in the same document, with my own emphasis:

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing (i.e. rightmost) dimensions and works its way left. Two dimensions are compatible when

  • they are equal, or
  • one of them is 1

If these conditions are not met, a ValueError: operands could not be broadcast together exception is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the size that is not 1 along each axis of the inputs.

Arrays do not need to have the same number of dimensions. For example, if you have a 256x256x3 array of RGB values, and you want to scale each color in the image by a different value, you can multiply the image by a one-dimensional array with 3 values. Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, shows that they are compatible:

Image  (3d array): 256 x 256 x 3
Scale  (1d array):             3
Result (3d array): 256 x 256 x 3

When either of the dimensions compared is one, the other is used. In other words, dimensions with size 1 are stretched or “copied” to match the other.

Now, let's try applying this logic to the example data.

A (2d array): 4 x 1
B (2d array): 3 x 1

Look at the first dimension: the lengths are 4 and 3. Is 4 equal to 3? No. Is either of those equal to 1? No. Therefore, the conditions are not met. We cannot broadcast along the first dimension of the array because there is not a rule that tells us how to match up 4 values against 3. If it were 4 values against 4, or 3 against 3, we could pair them up directly. If it were 4 against 1, or 1 against 3, we could "broadcast" by repeating the single value. Neither case applies here.

We could, however, multiply if either of the arrays were transposed:

A.T (2d array): 1 x 4
B   (2d array): 3 x 1

A   (2d array): 4 x 1
B.T (2d array): 1 x 3

Verifying this is left as an exercise for the reader.

  • Related