Last updated on:
Scenario
We are so acquainted with scalar calculations that we can do it with our eyes closed. But what if the arguments passed to a function are arrays? What if the operands beside an arithmetic operator are arrays? We'll need to consider not only how every element is calculated but also how the shapes of the arrays are handled. Universal functions and broadcasting show us how calculations are applied to arrays in NumPy.
Universal functions process all elements in an array at once.
Broadcasting shows how each element is calculated and how shapes of arrays are adjusted during arithmetic operations.
They are powerful features of NumPy. In nature, they extend scalar calculations to array and offer significant convenience for data processing. We'll work on some examples to see how they work. But we will not touch likes of performance, memory consuming etc. How they function is the scope that we are going to check out.
Example
We use Jupyter Lab for the demonstration.
Universal Functions
Noted the array doesn't have to be a NumPy array, it can be a normal one in Python.
List Case: y1 is a normal list in Python.
Array Case: y is a NumPy array.
ufunc.reduce method
ufunc.at method
Broadcasting
Case: Scalar-Array
Case: List-Array
Case: Arrays with the Same Shape
Case: Arrays with Compatible Shapes
Case: Arrays with Incompatible Shapes
Broadcasting works well only when the shapes of the arrays are compatible. If not, a ValueError is raised as shown above. How does NumPy determine whether they are compatible or not? Here are the general rules.
NumPy starts working from the rightmost dimensions toward left. If the two dimensions of the arrays satisfy the following conditions, they are compatible:
They are the same;Or either of them is 1.
No comments:
Post a Comment