Saturday, August 12, 2023

Python Tips - Universal Functions And Broadcasting In NumPy

  

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.


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



Let's have a quick look at Ufunc Methods as well, which are encapsulated along with universal functions. Some are support for binary functions while some for unary functions.

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 Functions
Broadcasting


No comments:

Post a Comment

React - Makeover in React: W3Schools How To

When it comes to building a React app, I get used to searching for a UI library to build the front pages, a straightforward way to achieve f...