
- 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 - Stacking Arrays
Stacking NumPy Array
Stacking arrays in NumPy refers to combining multiple arrays along a new dimension, creating higher-dimensional arrays. This is different from concatenation, which combines arrays along an existing axis without adding new dimensions.
NumPy provides several functions to achieve stacking. They are as follows −
- Using numpy.stack() Functiom
- Using numpy.vstack() Function
- Using numpy.hstack() Function
- Using numpy.dstack() Function
- Using numpy.column_stack() Function
Stacking Arrays Using stack() Function
We can use the stack() function in NumPy to stack a sequence of arrays along a new axis, creating a new dimension in the result.
Unlike numpy.concatenate() function, which combines arrays along an existing axis, numpy.stack() function adds a new axis at the specified position to the arrays being stacked.
Following is the syntax of the stack() function in NumPy −
np.stack(arrays, axis=0)
Where,
arrays − A sequence of arrays to be stacked.
axis − The axis along which to stack the arrays. The default is 0, which adds a new first axis.
Example: Stacking 1D Arrays
In the below example, we are stacking three 1D arrays along a new axis (axis 0) using the numpy.stack() function, resulting in a 2D array −
import numpy as np # arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr3 = np.array([7, 8, 9]) # Stack arrays along a new axis stacked_arr = np.stack((arr1, arr2, arr3), axis=0) print("Stacked Array along a new axis (Axis 0):") print(stacked_arr)
Following is the output obtained −
Stacked Array along a new axis (Axis 0): [[1 2 3] [4 5 6] [7 8 9]]
Example: Changing the Axis
The "axis" parameter in numpy.stack() function determines where the new axis is inserted. By changing the value of axis, you can control how the arrays are stacked −
import numpy as np # arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr3 = np.array([7, 8, 9]) # Stack arrays along axis 1 stacked_arr = np.stack((arr1, arr2, arr3), axis=1) print("Stacked Array along Axis 1:") print(stacked_arr)
This will produce the following result −
Stacked Array along Axis 1: [[1 4 7] [2 5 8] [3 6 9]]
Example: Stacking Multi-dimensional Arrays
The numpy.stack() function can also be used to stack multi-dimensional arrays. The function adds a new axis to the higher-dimensional arrays and stacks them accordingly.
In here, we are stacking two 2D arrays −
import numpy as np # 2D arrays arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) # Stack arrays along a new axis stacked_arr = np.stack((arr1, arr2), axis=0) print("Stacked 2D Arrays along a new axis (Axis 0):") print(stacked_arr)
Following is the output of the above code −
Stacked 2D Arrays along a new axis (Axis 0): [[[1 2] [3 4]] [[5 6] [7 8]]]
Stacking Arrays Using column_stack() Function
The numpy.column_stack() function in NumPy is used to stack 1D arrays as columns into a 2D array or to stack 2D arrays column-wise. This function provides a way to combine arrays along the second axis (axis=1), effectively increasing the number of columns in the resulting array.
Following is the syntax −
np.column_stack(tup)
Where, tup is a tuple of arrays to be stacked. The arrays can be either 1D or 2D, but they must have the same number of rows.
Example: Stacking 1D arrays as columns
In the example below, we are stacking two two 1D arrays as columns into a 2D array using the NumPy column_stack() function −
import numpy as np # 1D arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Column-stack 1D arrays stacked_arr_1d = np.column_stack((arr1, arr2)) print("Stacked 1D arrays as 2D array:") print(stacked_arr_1d)
We get the output as shown below −
Stacked 1D arrays as 2D array: [[1 4] [2 5] [3 6]]
Example: Stacking 2D arrays column-wise
In here, we are stacking two 2D arrays column-wise using the NumPy column_stack() function −
import numpy as np # 2D arrays arr3 = np.array([[1, 2], [3, 4]]) arr4 = np.array([[5, 6], [7, 8]]) # Column-stack 2D arrays stacked_arr_2d = np.column_stack((arr3, arr4)) print("Stacked 2D arrays column-wise:") print(stacked_arr_2d)
Following is the output obtained −
Stacked 2D arrays column-wise: [[1 2 5 6] [3 4 7 8]]
Vertical Stacking
We can also stack arrays vertically (row-wise) using the vstack() function in NumPy. It is equivalent to using numpy.concatenate() function with "axis=0", where arrays are concatenated along the first axis.
This results in an array with an increased number of rows, combining multiple arrays row-wise. Following is the syntax −
numpy.vstack(tup)
Where, tup is a tuple of arrays to be stacked vertically. All arrays must have the same number of columns.
Example
In the example below, we are stacking two arrays vertically using the NumPy vstack() function −
import numpy as np # arrays arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[7, 8, 9], [10, 11, 12]]) # Stack arrays vertically stacked_arr = np.vstack((arr1, arr2)) print("Vertically Stacked Array:") print(stacked_arr)
The output obtained is as shown below −
Vertically Stacked Array: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
Horizontal Stacking
We can stack arrays horizontally (column-wise) using the hstack() function in NumPy. It is equivalent to using numpy.concatenate() function with "axis=1", where arrays are concatenated along the second axis for 2D arrays.
This results in an array with an increased number of columns, combining multiple arrays column-wise. Following is the syntax −
numpy.hstack(tup)
Where, tup is a tuple of arrays to be stacked horizontally. All arrays must have the same number of rows.
Example
In the example below, we are stacking two arrays horizontally using the NumPy hstack() function −
import numpy as np # arrays arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) # Stack arrays horizontally stacked_arr = np.hstack((arr1, arr2)) print("Horizontally Stacked Array:") print(stacked_arr)
After executing the above code, we get the following output −
Horizontally Stacked Array: [[1 2 5 6] [3 4 7 8]]
Depth Stacking
The numpy.dstack() function is used to stack arrays along the third dimension, also known as the depth dimension. This combines arrays depth-wise, effectively creating a new dimension in the resulting array.
It is particularly useful when you want to combine multiple 2D arrays into a single 3D array. Following is the syntax −
np.dstack(tup)
Where, tup is a tuple of arrays to be stacked along the third dimension. All arrays must have the same shape in the first two dimensions.
Example
In this example, we are stacking two arrays along the third dimension using the NumPy dstack() function −
import numpy as np # arrays arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) # Stack arrays along the third dimension stacked_arr = np.dstack((arr1, arr2)) print("Depth-wise Stacked Array:") print(stacked_arr)
The result produced is as follows −
Depth-wise Stacked Array: [[[1 5] [2 6]] [[3 7] [4 8]]]