
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Broadcasting
NumPy Broadcasting
Broadcasting in NumPy refers to the ability of performing operations on arrays with different shapes by automatically expanding the smaller array's shape to match the larger array's shape. This is useful when performing arithmetic operations or applying functions to arrays of different dimensions.
When performing arithmetic operations, NumPy operates on corresponding elements of the arrays. If the arrays have the same shape, operations are are smoothly performed. However, if the arrays have different shapes, NumPy uses broadcasting to align them, allowing element-wise operations to be conducted easily.
Rules of Broadcasting
For broadcasting to work, the following rules must be satisfied −
- If arrays have a different number of dimensions, the shape of the smaller-dimensional array is padded with ones on the left side until both shapes have the same length.
- The size of each dimension must either be the same or one of them must be one.
- Broadcasting is applied from the last dimension to the first dimension.
For instance, consider two arrays with shapes (3, 4) and (4,). The broadcasting rules will align the shapes as follows −
Pad the smaller array's shape: The smaller array shape (4,) is padded to (1, 4).
Align dimensions: The shapes (3, 4) and (1, 4) are aligned as (3, 4) and (3, 4) respectively.
Perform element-wise operation: The operation is applied to the aligned shapes.
Adding a Scalar to an Array
When adding a scalar to an array, NumPy uses broadcasting to apply the scalar to each element of the array. Broadcasting expands the scalar to match the shape of the array, enabling element-wise operations.
Example
In the following example, we are broadcasting the scalar "10" to each element of the array, resulting in each element being increased by 10 −
import numpy as np # Creating an array array = np.array([[1, 2, 3], [4, 5, 6]]) # Adding a scalar result = array + 10 print(result)
Following is the output obtained −
[[11 12 13][14 15 16]]
Adding Arrays of Different Shapes
When adding arrays of different shapes, NumPy applies broadcasting rules to make their shapes compatible. Broadcasting works by stretching the smaller array across the larger one, so that both arrays have the same shape for element-wise addition.
This process eliminates the need for manually reshaping arrays before performing operations.
Example 1
In this example, we broadcast the second array "array2" to match the shape of the first array "array1" −
import numpy as np # Creating two arrays with different shapes array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([10, 20, 30]) # Adding arrays with broadcasting result = array1 + array2 print(result)
This will produce the following result −
[[11 22 33] [14 25 36]]
Example 2
Following is another example to broadcast two arrays with different shapes in NumPy −
import numpy as np a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]]) b = np.array([1.0,2.0,3.0]) print ('First array:') print (a) print ('\n') print ('Second array:') print (b) print ('\n') print ('First Array + Second Array') print (a + b)
The output of this program would be as follows −
First array: [[ 0. 0. 0.] [10. 10. 10.] [20. 20. 20.] [30. 30. 30.]] Second array: [1. 2. 3.] First Array + Second Array [[ 1. 2. 3.] [11. 12. 13.] [21. 22. 23.] [31. 32. 33.]]
The following figure demonstrates how array b is broadcast to become compatible with a.

Broadcasting with Multi-Dimensional Arrays
When performing operations between multi-dimensional arrays with different shapes, broadcasting rules align their dimensions so that they can be operated on element-wise.
This process involves stretching the smaller array to match the shape of the larger one, enabling operations to be performed smoothly.
Example
In the following example, we are creating two 3D arrays and then adding them with broadcasting −
import numpy as np # Creating two 3D arrays array1 = np.ones((2, 3, 4)) array2 = np.arange(4) # Adding arrays with broadcasting result = array1 + array2 print(result)
Following is the output of the above code −
[[[1. 2. 3. 4.] [1. 2. 3. 4.] [1. 2. 3. 4.]] [[1. 2. 3. 4.] [1. 2. 3. 4.] [1. 2. 3. 4.]]]
Applying Functions with Broadcasting
Broadcasting not only simplifies arithmetic operations between arrays of different shapes but also allows functions to be applied across arrays. These functions can include −
Mathematical Functions: Functions that perform mathematical operations, such as addition, subtraction, multiplication, and division.
Statistical Functions: Functions that compute statistical properties, like mean, median, variance, and standard deviation.
Reduction Functions: Functions that reduce the dimensions of an array by performing operations such as sum, product, or maximum.
Logical Operations: Functions that perform logical operations, such as comparisons and logical operations (e.g., AND, OR, NOT).
When applying functions to arrays with different shapes, broadcasting ensures that the function is applied element-wise.
Example
In this example, we use the numpy.maximum() function to perform an element-wise comparison between two arrays. The function compares each element of "array1" with the corresponding element of "array2", and the result is an array where each element is the maximum of the corresponding elements from the input arrays −
import numpy as np # Creating arrays array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([[10], [20]]) # Applying a function with broadcasting result = np.maximum(array1, array2) print(result)
After executing the above code, we get the following output −
[[10 10 10] [20 20 20]]