Array Stacking
Master NumPy array stacking (stack, vstack, hstack, dstack, column_stack) for efficient ML/AI data manipulation and model building.
Stacking Arrays in NumPy
Stacking in NumPy refers to combining multiple arrays along a new axis, thereby increasing the dimensionality of the resulting array. This is distinct from concatenation, which merges arrays along an existing axis.
NumPy offers several efficient functions for array stacking:
numpy.stack()
numpy.vstack()
numpy.hstack()
numpy.dstack()
numpy.column_stack()
1. numpy.stack()
The numpy.stack()
function creates a new array by stacking a sequence of input arrays along a new axis. This effectively increases the dimensionality of the output.
Syntax:
numpy.stack(arrays, axis=0)
arrays
: A sequence of array-like objects, all of which must have the same shape.axis
: An integer specifying the index of the new axis in the dimensions of the resulting array.
Example: Stacking 1D Arrays (Axis 0)
When axis=0
, the arrays are stacked along the first dimension.
import numpy as np
arr1 = np.array([10, 20, 30])
arr2 = np.array([40, 50, 60])
arr3 = np.array([70, 80, 90])
stacked = np.stack((arr1, arr2, arr3), axis=0)
print("Stacked along Axis 0:")
print(stacked)
Output:
Stacked along Axis 0:
[[10 20 30]
[40 50 60]
[70 80 90]]
Example: Stacking 1D Arrays (Axis 1)
When axis=1
, the arrays are stacked along the second dimension.
import numpy as np
arr1 = np.array([10, 20, 30])
arr2 = np.array([40, 50, 60])
arr3 = np.array([70, 80, 90])
stacked = np.stack((arr1, arr2, arr3), axis=1)
print("Stacked along Axis 1:")
print(stacked)
Output:
Stacked along Axis 1:
[[10 40 70]
[20 50 80]
[30 60 90]]
Example: Stacking 2D Arrays
When stacking 2D arrays, axis=0
inserts the new axis as the first dimension, creating a 3D array.
import numpy as np
arr1 = np.array([[1, 3], [5, 7]])
arr2 = np.array([[9, 11], [13, 15]])
stacked = np.stack((arr1, arr2), axis=0)
print("Stacked 2D Arrays along Axis 0:")
print(stacked)
Output:
Stacked 2D Arrays along Axis 0:
[[[ 1 3]
[ 5 7]]
[[ 9 11]
[13 15]]]
2. numpy.column_stack()
The numpy.column_stack()
function treats 1D arrays as columns to be stacked into a 2D array. If 2D arrays are provided, they are stacked column-wise (along axis=1
).
Syntax:
numpy.column_stack(tup)
tup
: A tuple of array-like objects.
Example: Stacking 1D Arrays as Columns
Each 1D array becomes a column in the resulting 2D array.
import numpy as np
arr1 = np.array([2, 4, 6])
arr2 = np.array([8, 10, 12])
stacked = np.column_stack((arr1, arr2))
print("1D Arrays Stacked as Columns:")
print(stacked)
Output:
1D Arrays Stacked as Columns:
[[ 2 8]
[ 4 10]
[ 6 12]]
Example: Column-wise Stacking of 2D Arrays
For 2D arrays, column_stack
appends columns from the second array to the first.
import numpy as np
arr1 = np.array([[1, 3], [5, 7]])
arr2 = np.array([[9, 11], [13, 15]])
stacked = np.column_stack((arr1, arr2))
print("2D Arrays Stacked Column-wise:")
print(stacked)
Output:
2D Arrays Stacked Column-wise:
[[ 1 3 9 11]
[ 5 7 13 15]]
3. numpy.vstack()
(Vertical Stacking)
The numpy.vstack()
function stacks arrays vertically, meaning it appends the arrays as new rows. This increases the number of rows in the resulting array.
Syntax:
numpy.vstack(tup)
tup
: A tuple of array-like objects. All arrays must have the same shape alongaxis=1
.
Example: Vertical Stacking
The second array is placed below the first array.
import numpy as np
arr1 = np.array([[10, 20, 30], [40, 50, 60]])
arr2 = np.array([[70, 80, 90], [100, 110, 120]])
stacked = np.vstack((arr1, arr2))
print("Vertically Stacked Array:")
print(stacked)
Output:
Vertically Stacked Array:
[[ 10 20 30]
[ 40 50 60]
[ 70 80 90]
[100 110 120]]
4. numpy.hstack()
(Horizontal Stacking)
The numpy.hstack()
function stacks arrays horizontally, meaning it appends the arrays as new columns. This increases the number of columns in the resulting array.
Syntax:
numpy.hstack(tup)
tup
: A tuple of array-like objects. For 1D arrays, they are concatenated. For 2D arrays, they are stacked column-wise.
Example: Horizontal Stacking
The second array's columns are appended to the first array's columns.
import numpy as np
arr1 = np.array([[5, 10], [15, 20]])
arr2 = np.array([[25, 30], [35, 40]])
stacked = np.hstack((arr1, arr2))
print("Horizontally Stacked Array:")
print(stacked)
Output:
Horizontally Stacked Array:
[[ 5 10 25 30]
[15 20 35 40]]
5. numpy.dstack()
(Depth Stacking)
The numpy.dstack()
function stacks arrays along the third dimension (depth). This creates a 3D array where the new axis is the last dimension.
Syntax:
numpy.dstack(tup)
tup
: A tuple of array-like objects.
Example: Depth Stacking
Each pair of input arrays forms a "slice" along the new depth dimension.
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
stacked = np.dstack((arr1, arr2))
print("Depth-wise Stacked Array:")
print(stacked)
Output:
Depth-wise Stacked Array:
[[[1 5]
[2 6]]
[[3 7]
[4 8]]]
Summary of Stacking Functions
| Function | Description | | :------------------ | :----------------------------------------- | | np.stack()
| Stack along a new axis | | np.column_stack()
| Stack 1D/2D arrays as columns (axis=1) | | np.vstack()
| Stack vertically (new rows, axis=0 conceptually) | | np.hstack()
| Stack horizontally (new columns, axis=1 conceptually) | | np.dstack()
| Stack along depth (new axis at the end, axis=2 conceptually) |
These stacking methods are invaluable for constructing complex data structures from simpler components, particularly in fields such as data science, image processing, machine learning, and matrix operations.
SEO Keywords: NumPy array stacking, NumPy stack function, NumPy vertical stacking, NumPy horizontal stacking, NumPy column stack, NumPy depth stacking, NumPy array manipulation, stacking vs concatenation NumPy.