ASP.NET Tutorial: Understanding NumPy Broadcasting
Understanding NumPy Broadcasting
NumPy, short for Numerical Python, is a fundamental library for scientific computing in Python. It supports large multi-dimensional arrays and matrices and a collection of mathematical functions to operate on these arrays efficiently.
NumPy Broadcasting
Numpy broadcasting is a feature that allows element-wise operations on arrays of different shapes.
Rules of Broadcasting
1. Matching Dimensions
When operating on 2 arrays, numpy compares their shapes, starting from the trailing dimensions. They are compatible if they are equal or one of them is of size 1.
import numpy as np
a= np.array([1,2,3])
b= 2
res=a+b
print(res)
a is an array of one dimension and b is of size 1, if we add both of them then the resultant is 1* 3, and 2 is added in all the elements of an array a.
2. Shape Expansion
If the arrays have different shapes, the smaller array is virtually replicated along the dimensions to operate.
Here, c is an array of 2-D and d is of 1-D then d replicates itself to perform an arithmetic operation.
Advantages of Broadcasting
- Saves Memory
- A smaller array
- replicates itself to operate.
- Eliminates the need for Python Loops.
Arithmetic operations are performed automatically with the help of this library and need not to write the Python loops.
In some cases, Broadcasting stretches both arrays to form an output array larger than the initial arrays.
Here, r is an array of 2*1 dimensions and f is an array of 1*2 dimensions. After adding both the arrays, the resultant is 2*2 dimensions.
ASP.NET Core 9 Hosting Recommendation
HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.