NumPy Broadcasting Arrays in Python

Let’s assume that we have a large data set, each data is a list of parameters. In Numpy, we have a 2-D array, where each row is data and the number of rows is the size of the data set. Suppose we want to apply some sort of scaling to all these data every parameter gets its scaling factor or say every parameter is multiplied by some factor.

Just to have a clear understanding, let’s count calories in foods using a macro-nutrient breakdown. Roughly put, the caloric parts of food are made of fats (9 calories per gram), protein (4 CPG), and carbs (4 CPG). 

So if we list some foods (our data), and for each food list its macro-nutrient breakdown (parameters), we can then multiply each nutrient by its caloric value (apply scaling) to compute the caloric breakdown of every food item.

With this transformation, we can now compute all kinds of useful information. For example, what is the total number of calories present in some food, or given a breakdown of my dinner know how many calories did I get from protein and so on?

Example: Broadcasting Array- Element Wise Multiplication

In this example, the code initializes a 2D array ‘macros’ representing nutritional values. 

It then creates a new array ‘result’ filled with zeros, having the same shape as ‘macros.’ 

Another array ‘cal_per_macro’ is defined. Finally, it multiplies each row of ‘macros’ by ‘cal_per_macro,’ storing the results in ‘result,’.

Python3




macros = array([
  [0.8, 2.9, 3.9],
  [52.4, 23.6, 36.5],
  [55.2, 31.7, 23.9],
  [14.4, 11, 4.9]
   ])
  
result = zeros_like(macros)
  
cal_per_macro = array([3, 3, 8])
  
 for i in range(macros.shape[0]):
     result[i, :] = macros[i, :] * cal_per_macro
  
result


Output: 

array([[   2.4,    8.7,   31.2 ],
       [  157.2,   70.8,  292 ],
       [   165.6,  95.1,   191.2],
       [   43.2,   33,    39.2]])

NumPy Array Broadcasting

The term broadcasting refers to the ability of NumPy to treat arrays with different dimensions during arithmetic operations. This process involves certain rules that allow the smaller array to be ‘broadcast’ across the larger one, ensuring that they have compatible shapes for these operations.

Broadcasting is not limited to two arrays; it can be applied over multiple arrays as well.

In this article, we will learn about broadcast over multiple arrays in the NumPy extension in Python.

Similar Reads

What is NumPy Array Broadcasting?

Broadcasting provides a means of vectorizing array operations, therefore eliminating the need for Python loops. This is because NumPy is implemented in C Programming, which is a very efficient language....

NumPy Broadcasting Arrays in Python

...

Array Broadcasting Algorithm

Let’s assume that we have a large data set, each data is a list of parameters. In Numpy, we have a 2-D array, where each row is data and the number of rows is the size of the data set. Suppose we want to apply some sort of scaling to all these data every parameter gets its scaling factor or say every parameter is multiplied by some factor....

Array Broadcasting Examples

...

Conclusion

The NumPy array broadcasting algorithm determines how NumPy will treat arrays with different shapes during arithmetic operations....

Contact Us