Why the following operands could not be broadcasted together?
Asked Answered
A

1

6

The arrays are of following dimensions: dists: (500,5000) train: (5000,) test:(500,)

Why does the first two statements throw an error whereas the third one works fine?

  1. dists += train + test

Error: ValueError: operands could not be broadcast together with shapes (5000,) (500,)

  1. dists += train.reshape(-1,1) + test.reshape(-1,1)

Error: ValueError: operands could not be broadcast together with shapes (5000,1) (500,1)

  1. dists += train + test.reshape(-1,1) This works fine!

Why does this happen?

Admiralty answered 8/6, 2018 at 10:3 Comment(0)
G
6

It's to do with NumPy's broadcasting rules. Quoting the NumPy manual:

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

  1. they are equal, or
  2. one of them is 1

The first statement throws an error because NumPy looks at the only dimension, and (5000,) and (500,) are inequal and cannot be broadcast together.

In the second statement, train.reshape(-1,1) has the shape (5000,1) and test.reshape(-1,1) has the shape (500,1). The trailing dimension (length one) is equal, so that's ok, but then NumPy checks the other dimension and 5000 != 500, so the broadcasting fails here.

In the third case, your operands are (5000,) and (500,1). In this case NumPy does allow broadcasting. The 1D-array is extended along the trailing length-1 dimension of the 2D-array.

FWIW, the shape and broadcasting rules can be a bit tricky sometimes, and I've often been confused with similar matters.

Griner answered 8/6, 2018 at 10:41 Comment(2)
Is it the case that because both of them are 1 it needed to check if they were equal, but when ONLY one of them is 1 it doesn't need to check for equality?Gilding
It needs to check every dimension for compatibility. In the second case 1 and 1 are compatible, but 5000 and 500 are not. In the third case 5000 and 1 are compatible, and there are no more dimensions to check.Griner

© 2022 - 2024 — McMap. All rights reserved.