0
0
NumPydata~5 mins

Scalar and array broadcasting in NumPy

Choose your learning style9 modes available
Introduction

Broadcasting lets you do math between arrays of different sizes easily. It saves time and code by automatically expanding smaller arrays.

Adding a single number to every element in a list of numbers.
Multiplying each row of a table by a different number.
Subtracting the average value from each element in a dataset.
Scaling all values in an array by a constant factor.
Applying a formula to each element without writing loops.
Syntax
NumPy
import numpy as np

# Example: Add scalar to array
array = np.array([1, 2, 3])
scalar = 5
result = array + scalar

# Example: Add arrays with broadcasting
array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([10, 20, 30])
result = array1 + array2

Broadcasting happens automatically when shapes are compatible.

A scalar is treated like an array with shape ().

Examples
Adds 10 to each element of the array.
NumPy
import numpy as np

# Scalar added to array
array = np.array([1, 2, 3])
scalar = 10
result = array + scalar
print(result)
Broadcasts 1D array across rows of 2D array.
NumPy
import numpy as np

# Adding 1D array to 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
array_1d = np.array([10, 20, 30])
result = array_2d + array_1d
print(result)
Adding scalar to empty array returns empty array.
NumPy
import numpy as np

# Edge case: empty array
empty_array = np.array([])
scalar = 5
result = empty_array + scalar
print(result)
Scalar multiplies the single element.
NumPy
import numpy as np

# Edge case: single element array
single_element_array = np.array([100])
scalar = 3
result = single_element_array * scalar
print(result)
Sample Program

This program shows how a 1D array is added to each row of a 2D array by broadcasting. It also shows adding a scalar to every element.

NumPy
import numpy as np

# Create a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("Original 2D array:")
print(array_2d)

# Create a 1D array to broadcast
array_1d = np.array([10, 20, 30])
print("\n1D array to broadcast:")
print(array_1d)

# Add 1D array to 2D array using broadcasting
result_add = array_2d + array_1d
print("\nResult of adding 1D array to 2D array:")
print(result_add)

# Add scalar to 2D array
scalar_value = 5
result_scalar = array_2d + scalar_value
print("\nResult of adding scalar to 2D array:")
print(result_scalar)
OutputSuccess
Important Notes

Time complexity is O(n) where n is number of elements in the largest array.

Space complexity depends on the output array size, usually same as the largest input array.

Common mistake: shapes must be compatible for broadcasting, otherwise error occurs.

Use broadcasting to avoid writing loops and make code faster and cleaner.

Summary

Broadcasting lets you do math between arrays of different shapes easily.

Scalars act like arrays with no dimensions and can be combined with any array.

Always check array shapes to ensure broadcasting will work without errors.