It is impossible to carry out certain arithmetic operations if two arrays have different shapes. In this situation, Python provides you with a built-in numpy.array()
broadcasting feature that broadcasts the smaller array across the larger array.
Take a look at the example below:
As stated earlier, we will broadcast
the smaller array across the larger array to perform this arithmetic operation.
One of the possible cases (shown above) that makes broadcasting possible is when two matrices have the same columns. However​, we have two more cases:
# importing numpy libraryimport numpy as np# MatA is a 3x3 matrixMatA = np.array([[1,2,3],[4,5,6],[7,8,9]])# MatB is also a 1x3 matrixMatB = np.array([[1],[2],[3]])# Arithmetic operation is possible because# of broadcastingprint (MatA + MatB)
Free Resources