Saturday, August 12, 2023

[Tips] Python - Universal Functions And Broadcasting In NumPy

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.


Exceptional Case: what if an error takes place while calculating an element?



Let's have a quick look at Ufunc Methods as well, which are encapsulated along with universal functions. Some methods support universal functions which operate on two operands, while some are for functions which process only one operand.

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

React - Makeover in React: W3Schools How To

Last updated on: When it comes to building a React app, I get used to searching for a UI library to build the front pages, a straightforwar...