Last Updated : 11 Jul, 2025
Broadcasting in NumPy allows us to perform arithmetic operations on arrays of different shapes without reshaping them. It automatically adjusts the smaller array to match the larger array's shape by replicating its values along the necessary dimensions. This makes element-wise operations more efficient by reducing memory usage and eliminating the need for loops. In this article, we will see how broadcasting works.
Lets see an example:
Python
import numpy as np
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 10
result = array_2d + scalar
print(result)
Output:
[[11 12 13]
[14 15 16]]
In this example, the scalar value 10 is broadcasted to match the shape of the 2D array. As a result the scalar 10 is added to each element of the 2D array creating a new array where each element is increased by 10 which results in the output. This process simplifies element-wise operations and improves efficiency by avoiding explicit loops.
Working of Broadcasting in NumPyBroadcasting applies specific rules to find whether two arrays can be aligned for operations or not that are:
If these conditions aren’t met NumPy will raise a ValueError.
Lets see various examples for broadcasting below:
Example 1: Broadcasting a Scalar to a 1D ArrayIt creates a NumPy array arr with values [1, 2, 3]. It adds a scalar value 1 to each element of the array using broadcasting.
Python
import numpy as np
arr = np.array([1, 2, 3])
res = arr + 1
print(res)
Output:
Example 2: Broadcasting a 1D Array to a 2D Array[2 3 4]
This example shows how a 1D array a1 is added to a 2D array a2. NumPy automatically expands the 1D array along the rows of the 2D array to perform element-wise addition.
Python
import numpy as np
a1 = np.array([2, 4, 6])
a2 = np.array([[1, 3, 5], [7, 9, 11]])
res = a1 + a2
print(res)
Output:
[[ 3 7 11]
[ 9 13 17]]
It allows us to perform addition on arrays of different shapes without needing to reshape them.
Example 3: Broadcasting in Conditional OperationsWe may need to apply a condition to an entire array or a subset of it. Broadcasting can help to perform these operations efficiently without needing loops.
Python
import numpy as np
ages = np.array([12, 24, 35, 45, 60, 72])
age_group = np.array(["Adult", "Minor"])
result = np.where(ages > 18, age_group[0], age_group[1])
print(result)
Output:
['Minor' 'Adult' 'Adult' 'Adult' 'Adult' 'Adult']
In this example, broadcasting is used to efficiently check which elements in the ages array are greater than 18. Instead of looping through the array the condition is applied across the entire array using NumPy’s where() function. Result is an array of 'Adult' or 'Minor' based on the ages.
Example 4: Using Broadcasting for Matrix MultiplicationIn this example, each element of a 2D matrix is multiplied by the corresponding element in a broadcasted vector.
Python
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
vector = np.array([10, 20])
result = matrix * vector
print(result)
Output:
[[10 40]
[30 80]]
The 1D vector is broadcasted across the rows of the 2D matrix matrix. This allows element-wise multiplication of corresponding elements between the matrix and the vector.
Example 5: Scaling Data with BroadcastingConsider a real-world scenario where we need to calculate the total calories in foods based on the amount of fats, proteins and carbohydrates. Each nutrient has a specific caloric value per gram.
Left table shows the original data with food items and their respective grams of fats, proteins and carbs. The array [3, 3, 8] represents the caloric values per gram for fats, proteins and carbs respectively. This array is being broadcast to match the dimensions of the original data and arrow indicates the broadcasting operation.
import numpy as np
food_data = np.array([[0.8, 2.9, 3.9],
[52.4, 23.6, 36.5],
[55.2, 31.7, 23.9],
[14.4, 11, 4.9]])
caloric_values = np.array([3, 3, 8])
caloric_matrix = caloric_values
calorie_breakdown = food_data * caloric_matrix
print(calorie_breakdown)
Output:
Scaling Data with BroadcastingThe caloric values per gram are broadcasted across each row of the food data allowing us to find the calorie breakdown efficiently.
Example 6: Adjusting Temperature Data Across Multiple LocationsSuppose you have a 2D array representing daily temperature readings across multiple cities and you want to apply a correction factor to each city’s temperature data.
Python
import numpy as np
temperatures = np.array([
[30, 32, 34, 33, 31],
[25, 27, 29, 28, 26],
[20, 22, 24, 23, 21]
])
corrections = np.array([1.5, -0.5, 2.0])
adjusted_temperatures = temperatures + corrections[:, np.newaxis]
print(adjusted_temperatures)
Output:
Adjusting Temperature DataThe correction factors for each city are broadcasted to match the shape of the temperatures array.
Example 7: Normalizing Image DataNormalization is important in many real-world scenarios like image processing and machine learning because it:
Let's see how broadcasting simplifies normalization:
Python
import numpy as np
image = np.array([
[100, 120, 130],
[90, 110, 140],
[80, 100, 120]
])
mean = image.mean(axis=0)
std = image.std(axis=0)
normalized_image = (image - mean) / std
print(normalized_image)
Output:
Normalizing Image Data Example 8: Centering Data in Machine LearningCentering data is an important step in many machine learning workflows. Broadcasting helps center the data efficiently by subtracting the mean from each feature.
Python
import numpy as np
data = np.array([
[10, 20],
[15, 25],
[20, 30]
])
feature_mean = data.mean(axis=0)
centered_data = data - feature_mean
print(centered_data)
Output:
Centering Data in Machine LearningIt allows us to subtract the mean of each feature from the corresponding values in the data helps in efficiently centering the data without needing loops.
Broadcasting in NumPy helps in efficient data manipulation which makes complex operations on arrays of different shapes both straightforward and computationally efficient.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4