• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing NumPy broadcast() Function in Python

NumPy broadcast() function in Python is used to return an object that mimics broadcasting. It describes the ability of NumPy to treat arrays of different shapes during arithmetic operations. NumPy arithmetic operations are usually done on pairs of arrays on an element-by-element basis. In the simplest case, the two arrays must have exactly the same shape, then these operations will smoothly perform. Arrays with different sizes cannot add, subtract, or generally used in arithmetic.


To overcome this problem by duplicating the smaller array that is, it contains the same dimensions and size as the larger array. This process is called array broadcasting and is available in NumPy when performing array arithmetic, which can greatly reduce and simplify your code. In this tutorial, you will learn the concept of array broadcasting and how to implement it in Python NumPy.

1. Quick Examples of broadcast() Function

If you are in a hurry, below are some quick examples of the Python NumPy broadcast() function.

# Quick examples of broadcast() function

# Example 1: Add two arrays 
# Using broadcasting
arr1 = np.array([10, 20,30]) # (1,3)
arr2 = np.array([40]) # (1,)
arr = arr1 + arr2

# Example 2: Add two arrays 
# Using broadcast rule
arr = np.array([[4], [7], [12]])
arr1 = np.array([3, 6, 9])
arr2 = np.broadcast(arr, arr1)
out = np.empty(arr2.shape)
out.flat = [u+v for (u,v) in arr2]

# Example 3: Add two arrays 
# Using broadcasting
arr = np.array([[4], [7], [12]])
arr1 = np.array([3, 6, 9])
ar r= arr1 + arr2

2. Syntax of numpy.broadcast()

Following is the syntax of numpy.broadcast().

# Syntax of numpy.broadcast()
numpy.broadcast(in1, in2, …)

2.2 Parameters of broadcast() Function

Following are the parameters of the broadcast() function.

  • in1,in2, … These are the input parameters(like arrays).

2.3 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 use as an iterator.

3. Usage of NumPy broadcast()

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 possible 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 rules are satisfied

First rule: If the two arrays are having different dimensions, we should make equal dimensions. Padded 1’s in the shape of fewer dimension array on the left side. For example,

Here, I have declared two arrays named x, y :

x=(4,3),y =(3,) after applying the above rule the dimensions of x, y arrays changed into x=(4,3),y=(1,3). Now two arrays having same dimensions.

The second rule: If the size of two arrays are not same in any dimension, the array size equal to 1 in that dimension, is expanded to match other sizes of the same dimension.

x=(4,3),y = (1,3) after applying second rule the shapes of x, y arrays changed into x = (4,3), y = (4,3).

Now both arrays are having same dimensions and the same shape, the arithmetic operation will perform normally.

4. Use broadcast() on NumPy Arrays

In order to add two arrays, you have to satisfy broadcasting rules. You are using NumPy to perform addition between two arrays using broadcasting. For instance, arr1 has a shape of (3,), which means it is a 1-dimensional array with 3 elements. arr2 has a shape of (1,), which is also a 1-dimensional array with 1 element.

When you use the + operator between arr1 and arr2, NumPy automatically applies broadcasting rules. Broadcasting allows NumPy to perform element-wise operations on arrays of different shapes by implicitly expanding the smaller array to match the shape of the larger one. In this case, arr2 is broadcasted to the shape (3,) to match the shape of arr1. This is done by replicating the single element of arr2 along the corresponding dimension. The addition is then performed element-wise, resulting in the array arr with the shape (3,).

# Import numpy module
import numpy as np

# Create two input arrays
arr1 = np.array([10, 20,30]) # (1,3)
print("First array:",arr1)
arr2 = np.array([40]) # (1,)
print("Second array:",arr2)

# Add two arrays using broadcasting
arr = arr1 + arr2
print("Add two arrays using broadcasting:",arr)

Yields below output.

numpy broadcast

Here, happened the broadcast because of a miss-match in the array dimension.

Using numpy.broadcast() you can add the two arrays. For more complex operations, especially those that involve aggregations, reductions, or other non-element-wise manipulations, you might need to explore other functionalities in NumPy or consider alternative approaches.

For non-element-wise operations, you may need to explore other NumPy functions or implement custom logic based on your specific requirements. NumPy provides a wide range of functions for array manipulation, aggregation, and other operations, such as np.sum, np.mean, np.dot, etc.

# Add two arrays using broadcast()
arr = np.array([[4], [7], [12]])
arr1 = np.array([3, 6, 9])
arr2 = np.broadcast(arr, arr1)
out = np.empty(arr2.shape)
out.flat = [u+v for (u,v) in arr2]
print("Result after manual broadcasting:\n",out)

# OutPut:
# Result after manual broadcasting:
# [[ 7. 10. 13.]
#  [10. 13. 16.]
#  [15. 18. 21.]]

Alternatively, First imports the NumPy library and gives it the alias np. This is a common convention to make it easier to reference NumPy functions and objects in your code. To create a NumPy array arr1 with shape (3, 1). It’s a 2-dimensional array with three rows and one column. creates another NumPy array arr2 with shape (3,). It’s a 1-dimensional array with three elements.

Here, arr1 and arr2 have different shapes, but NumPy automatically broadcasts them to perform element-wise addition. Broadcasting rules allow arrays with different shapes to be combined in a way that makes sense. After broadcasting, the addition is performed element-wise, resulting in the final array arr.

# Create two input arrays
arr1 = np.array([[4], [7], [12]])
arr2 = np.array([3, 6, 9])

# Add two arrays using broadcasting
arr = arr1 + arr2
print("Add two arrays using broadcasting:\n",arr)
# Output:
# Add two arrays using broadcasting
# [[ 7 10 13]
#  [10 13 16]
#  [15 18 21]]

Frequently Asked Questions

What is the purpose of numpy.broadcast?

The numpy.broadcast function is used to create a broadcast object that encapsulates information about how NumPy performs broadcasting. Broadcasting is a feature that allows operations to be performed on arrays of different shapes and sizes, as long as they can be broadcasted to a common shape.

How does broadcasting work in NumPy?

Broadcasting in NumPy allows arrays with different shapes to be combined in a way that makes sense for element-wise operations. When operating on two arrays, NumPy compares their shapes element-wise. The dimensions are considered compatible when they are equal, or one of them is 1. If these conditions are not met, a ValueError is raised.

When should I use numpy.broadcast?

In most cases, you don’t need to use numpy.broadcast explicitly. NumPy performs broadcasting automatically when you perform element-wise operations on arrays of different shapes. numpy.broadcast is used internally by NumPy to handle the broadcasting process.

What is the difference between numpy.broadcast and standard broadcasting?

numpy.broadcast is a function that returns a broadcast object, while standard broadcasting refers to the automatic handling of broadcasting by NumPy when performing element-wise operations. In most cases, you can use standard broadcasting without explicitly creating a broadcast object using numpy.broadc

Can I use numpy.broadcast for non-element-wise operations?

numpy.broadcast is primarily designed for handling element-wise operations, and its main purpose is to facilitate broadcasting for operations like addition, subtraction, multiplication, etc., where the operation is applied element-wise across arrays of different shapes. If you have a non-element-wise operation, numpy.broadcast might not be the appropriate tool.


In this article, I have explained the concept of array broadcasting and how to implement it using the NumPy broadcast() function with examples.

Happy Learning!!