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 to 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 we are going to check out.
Example
We use JupyterLab 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
Scalar-Array Case
List-Array Case
Arrays with Same Shape Case
Arrays with Compatible Shapes Case
Arrays with Incompatible Shapes Case
Broadcasting works well only when the shapes of the arrays are compatible. If not, a ValueError is raised as shown above. How does NumPy judge whether they are compatible or not? NumPy starts working from the rightmost dimensions toward left. If the dimensions are equal or either is 1, they are compatible.
Reference
Universal FunctionsBroadcasting
No comments:
Post a Comment