🐍 NumPy – Stacking Arrays
Stacking arrays is a common operation in NumPy used to combine multiple arrays into a larger structure.
While concatenation joins arrays along an existing axis, stacking creates a new dimension or organizes arrays in different orientations.
Array stacking is widely used in:
- Data Science
- Machine Learning
- Deep Learning
- Image Processing
- Scientific Computing
NumPy provides several stacking functions that make combining arrays simple and efficient.
What is Array Stacking?
Array stacking means combining multiple arrays into a single array by arranging them:
- Horizontally
- Vertically
- Depth-wise
- Along a new axis
Example:
Array A:
[1, 2, 3]Array B:
[4, 5, 6]Stacked result:
[[1, 2, 3],
[4, 5, 6]]Using np.stack()
The stack() function joins arrays along a new axis.
Syntax
np.stack((array1, array2), axis=0)Basic Example
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.stack((a, b))
print(result)Output:
[[1 2 3]
[4 5 6]]Shape:
print(result.shape)Output:
(2, 3)Understanding the axis Parameter
The axis parameter determines where the new dimension is inserted.
axis = 0
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.stack((a, b), axis=0))Output:
[[1 2 3]
[4 5 6]]Shape:
(2, 3)axis = 1
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.stack((a, b), axis=1))Output:
[[1 4]
[2 5]
[3 6]]Shape:
(3, 2)Horizontal Stacking – hstack()
The hstack() function stacks arrays horizontally.
Example
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.hstack((a, b))
print(result)Output:
[1 2 3 4 5 6]hstack() with 2D Arrays
import numpy as np
a = np.array([
[1, 2],
[3, 4]
])
b = np.array([
[5, 6],
[7, 8]
])
print(np.hstack((a, b)))Output:
[[1 2 5 6]
[3 4 7 8]]Vertical Stacking – vstack()
The vstack() function stacks arrays vertically.
Example
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))Output:
[[1 2 3]
[4 5 6]]vstack() with Matrices
import numpy as np
a = np.array([
[1, 2],
[3, 4]
])
b = np.array([
[5, 6],
[7, 8]
])
print(np.vstack((a, b)))Output:
[[1 2]
[3 4]
[5 6]
[7 8]]Depth Stacking – dstack()
The dstack() function stacks arrays along the third dimension.
Example
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dstack((a, b)))Output:
[[[1 4]
[2 5]
[3 6]]]Shape:
print(np.dstack((a, b)).shape)Output:
(1, 3, 2)Using column_stack()
Creates columns from arrays.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.column_stack((a, b)))Output:
[[1 4]
[2 5]
[3 6]]Using row_stack()
Creates rows from arrays.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.row_stack((a, b)))Output:
[[1 2 3]
[4 5 6]]Stacking Multiple Arrays
import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
c = np.array([5, 6])
print(np.stack((a, b, c)))Output:
[[1 2]
[3 4]
[5 6]]Shape Requirements
All arrays must have identical shapes when using stack().
Valid:
(3,)
(3,)Invalid:
(3,)
(4,)Example:
a = np.array([1, 2, 3])
b = np.array([4, 5])
np.stack((a, b))Output:
ValueErrorStack vs Concatenate
| Feature | stack() | concatenate() |
|---|---|---|
| Creates new axis | Yes | No |
| Changes dimensions | Yes | No |
| Shape requirement | Same shape | Compatible shape |
| Use case | Build new dimensions | Merge existing arrays |
Real-World Example
Combining sensor data from multiple devices.
import numpy as np
sensor1 = np.array([10, 20, 30])
sensor2 = np.array([40, 50, 60])
data = np.column_stack((sensor1, sensor2))
print(data)Output:
[[10 40]
[20 50]
[30 60]]Result:
Time | Sensor1 | Sensor2Useful for analytics and machine learning datasets.
Common Stacking Functions
| Function | Purpose |
| stack() | New axis |
| hstack() | Horizontal stacking |
| vstack() | Vertical stacking |
| dstack() | Depth stacking |
| column_stack() | Create columns |
| row_stack() | Create rows |
Performance Tips
Avoid repeated stacking inside loops.
Instead of:
result = np.array([])
for i in range(100):
result = np.hstack((result, [i]))Use:
data = []
for i in range(100):
data.append(i)
result = np.array(data)This approach is much faster and uses less memory.
Best Practices
- Verify shapes before stacking.
- Use stack() when a new dimension is required.
- Use hstack() for horizontal merging.
- Use vstack() for vertical merging.
- Use column_stack() for dataset creation.
- Avoid excessive stacking in loops.
Common Errors
Shape Mismatch
ValueError:
all input arrays must have the same shapeCheck shapes:
print(a.shape)
print(b.shape)before stacking.
Summary
NumPy provides powerful stacking functions:
- stack()
- hstack()
- vstack()
- dstack()
- column_stack()
- row_stack()
These tools help organize and combine data efficiently for scientific computing and machine learning applications.
Conclusion
Array stacking is an essential NumPy skill that enables developers to organize multidimensional data efficiently. Understanding the differences between stack(), hstack(), vstack(), and dstack() helps you choose the right tool for building complex datasets and preparing data for machine learning models.


0 Comments