Python lists do not support direct vectorizing arithmetic operations. NumPy offers a faster-vectorized array operation compared to Python list loop-based operations. Here, all the looping operations are performed in C instead of Python, which makes it faster. Broadcasting functionality checks a set of rules for applying binary functions, such as addition, subtraction, and multiplication, on different shapes of an array.
Let's see an example of broadcasting:
# Create NumPy Array
arr1 = np.arange(1,5).reshape(2,2)
print(arr1)
Output:
[[1 2]
[3 4]]
# Create another NumPy Array
arr2 = np.arange(5,9).reshape(2,2)
print(arr2)
Output:
[[5 6]
[7 8]]
# Add two matrices
print(arr1+arr2)
Output:
[[ 6 8]
[10 12]]
In all three preceding examples, we can see the addition of two arrays of the same size. This concept is known as broadcasting:
# Multiply two matrices
print(arr1*arr2)
Output:
[[ 5 12]
[21 32]]
In the preceding example, two matrices were multiplied. Let's perform addition...