Let’s say I have a 2D Numpy array:
>>> a = np.random.random((4,6))
and I want to add a 1D array to each row:
>>> c = np.random.random((6,))
>>> a + c
This works. Now if I try adding a 1D array to each column, I get an error:
>>> b = np.random.random((4,))
>>> a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
I can fix this by using np.newaxis:
>>> a + b[:,np.newaxis]
which does work as expected.
What are the shape-matching rules to avoid having to use np.newaxis? Is it that the last element of the numpy shape tuple has to match? Does this rule also apply to higher dimensions? For example, the following works:
>>> a = np.random.random((2,3,4,5))
>>> b = np.random.random((4,5))
>>> a + b
So my question is whether this is documented anywhere, and if it is a behavior that can be relied on, or whether it is best to always use np.newaxis?
This is a distinctive feature of numpy called ‘broadcasting’:
if you can multiply a vector by a scalar why not allow multiplying a matrix by a vector? Just like every element of a vector is multiplied by a scalar in the first case, every cell in the matrix row is multiplied by the corresponding vector element in the second case.
Broadcasting is done using four rules which are a bit complicated in formulation but are rather intuitive once understood:
For example,

The operation is possible (doesn’t result in
shape mismatcherror you mentioned) in three cases:More examples can be found in my recent article on broadcasting [1] or in the official docs [2].
References