Python NumPy broadcast() Function

Numpy Module:

NumPy is a Python module that is used to work with arrays.

It also has functions for working with linear algebra, the Fourier transform, and matrices.

Travis Oliphant designed NumPy in 2005. It is an open-source project that you are free to use.

NumPy is an abbreviation for Numerical Python.

Uses of NumPy Module:

Lists in Python serve the same purpose as arrays, although they are slower to process.

NumPy’s goal is to provide array objects that are up to 50 times faster than ordinary Python lists.

The array object in NumPy is named ndarray, and it comes with a slew of helper methods that make working with ndarray a breeze.

Arrays are often utilized in data science, where speed and resources are critical.

NumPy broadcast() Function:

NumPy’s broadcast() function returns an object that mimics broadcasting.

The ability of NumPy to treat arrays of various shapes during arithmetic operations is referred to as broadcasting. Array arithmetic operations are typically performed on corresponding elements. If two arrays have the same shape, these operations are executed smoothly.

Syntax:

numpy.broadcast(in1, in2, …)

Parameters

in1, in2, …: This is required. These are the input parameters(like arrays).

Return Value:

Broadcast the input arguments against one another and return an object encapsulating the result. It has shape and nd attributes, among others, and can be used as an iterator.

Element-to-element operations are not possible if the dimensions of two arrays differ. Because of the broadcasting capabilities, operations on arrays of non-similar shapes are still feasible in NumPy. The smaller array is broadcast to the size of the larger array in order for their shapes to be compatible.

Broadcasting is possible if the following conditions are met:

  • The shape of an array with a smaller ndim than the other is prefixed with ‘1’.
  • The size of the output shape in each dimension is the maximum of the input sizes in that dimension.
  • An input can be used in a calculation if its size in a certain dimension corresponds to the output size or if its value is exactly 1.
  • If the dimension size of input is 1, the first data entry in that dimension is used for all calculations along that dimension.

NumPy broadcast() Function in Python

Approach:

  • Import numpy module using the import keyword.
  • Pass the list of lists as arguments to the array() function to create an array and store it in a variable.
  • Give the random list as an argument to the array() function to create the other array and store it in another variable.
  • Pass the above given two numpy arrays as arguments to the broadcast() function to broadcast the given two array elements against one another.
  • Add elements manually.
  • Create an empty numpy array using empty() by passing argument as broadcast shape.
  • Add both the values using the list comprehension.
  • Print the above result.
  • Compare against built-in broadcasting.
  • The Exit of the program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the list of lists as arguments to the array() function 
# to create an array and store it in a variable.
p = np.array([[2], [1], [4]])
# Give the random list as an argument to the array() function 
# to create the other array and store it in another variable.
q = np.array([7, 2, 3])
# Pass the above given two numpy arrays as arguments to the broadcast() function
# to broadcast the given two array elements against one another 
brdcast = np.broadcast(p, q)

# Adding elements manually
# creating an empty numpy array using empty() by passing argument as broadcast shape
rslt = np.empty(brdcast.shape)
# Adding both the values using the list comprehension
rslt.flat = [u+v for (u,v) in brdcast]

# Print the above result
print("Printing the above result:")
print(rslt)

# Comparing against built-in broadcasting
print("Comparing against built-in broadcasting:")
print(p+q)

Output:

Printing the above result:
[[ 9. 4. 5.]
[ 8. 3. 4.]
[11. 6. 7.]]
Comparing against built-in broadcasting:
[[ 9 4 5]
[ 8 3 4]
[11 6 7]]