Reputation: 413
I have the following Numpy array. And I write the following code :
import numpy as np
np_mat = np.array([[1, 2],[3, 4],[5, 6]])
np_mat * 2
print(np_mat.shape)
np_mat = np_mat + np.array([[10, 11],[-1,-2]])
print(np_mat)
Surprisingy Python throws an error saying
ValueError Traceback (most recent call last)
<ipython-input-23-557c7da40077> in <module>
5 np_mat * 2
6 print(np_mat.shape)
----> 7 np_mat = np_mat + np.array([[10, 11],[-1,-2]])
8 print(np_mat)
9 print(np.median(np_mat))
ValueError: operands could not be broadcast together with shapes (3,2) (2,2)
Again when I make a small modification to the code that is, the same snippet but just
np_mat = np_mat + np.array([10, 11])
it runs well showing the output
[[11 13]
[13 15]
[15 17]]
I didn't understand the difference, how does Numpy addition work inside? Is there any rule for it? I am pretty sure that Numpy addition/multiplications don't work like normal matrix operations in algebra. Then what's the trick?
Also, for a Numpy 2D array like
np_mat = np.array([[1, 2],
[3, 4],
[5, 6]])
when I write np.median(np_mat)
it says the result is 3.5. How is it possible that the median is a number when the array is 2D? I could see the same results when I do a multiplication : np_mat = np_mat * np.array([10, 11])
the output comes
[[10 22]
[30 44]
[50 66]]
Numpy seems baffling! Please assist. Thanks in advance.
Upvotes: 1
Views: 110
Reputation: 6327
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
When one of the dimensions is 1
, it will expand to the shape of the other array in the calculation.
In your first example the shapes are (3,2)
and (2,2)
so the right most dimension works, but the 3 and 2 are incompatible.
In the second example the shapes are (3,2)
and (1,2)
, so the right most dimension works and 3 is compatible with 1.
For the median example, just read the docs for what is happening:
axis{int, sequence of int, None}, optional Axis or axes along which the medians are computed. The default is to compute the median along a flattened version of the array.
By default, the median is calculated across all of the values in the array. However, you can select an axis that you would like to compute across.
Upvotes: 1
Reputation: 1175
If the arrays match in size along an axis, then elements will be operated on element-by-element, similar to how the built-in Python function zip() works. If one of the arrays has a size of 1 in an axis, then that value will be broadcast along that axis, or duplicated as many times as necessary to match the number of elements along that axis in the other array.
Please read more here
Upvotes: 1