*73*

Stacking is the process of combining multiple arrays along a specified axis.

In NumPy, stacking allows you to create new dimensions or merge arrays along existing dimensions, which is useful for data manipulation, preprocessing, and formatting in machine learning, scientific computing, and data analysis.

In this tutorial, we will cover:

- Vertical Stacking with vstack()
- Horizontal Stacking with hstack()
- Depth Stacking with dstack()
- Stacking Along New Axes with stack()
- Concatenating vs. Stacking

Let’s dive into each stacking method with examples.

## 1. Vertical Stacking with vstack()

The vstack() function stacks arrays vertically (row-wise), combining arrays along rows.

It’s useful for stacking 1D arrays as rows in a 2D array or for combining 2D arrays.

### Example 1: Vertical Stacking of 1D Arrays

import numpy as np # Create two 1D arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Stack them vertically vstacked_array = np.vstack((array1, array2)) print("Vertically Stacked 1D Arrays:\n", vstacked_array)

Output:

Vertically Stacked 1D Arrays: [[1 2 3] [4 5 6]]

- Explanation: vstack() combines array1 and array2 as rows in a 2D array.

### Example 2: Vertical Stacking of 2D Arrays

# Create two 2D arrays array2d_1 = np.array([[1, 2, 3], [4, 5, 6]]) array2d_2 = np.array([[7, 8, 9], [10, 11, 12]]) # Stack them vertically vstacked_2d = np.vstack((array2d_1, array2d_2)) print("\nVertically Stacked 2D Arrays:\n", vstacked_2d)

Output:

Vertically Stacked 2D Arrays: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]

- Explanation: vstack() adds array2d_2 below array2d_1, stacking rows.

## 2. Horizontal Stacking with hstack()

The hstack() function stacks arrays horizontally (column-wise).

It’s useful for concatenating 1D arrays as columns in a 2D array or for combining 2D arrays side-by-side.

### Example 3: Horizontal Stacking of 1D Arrays

# Stack two 1D arrays horizontally hstacked_array = np.hstack((array1, array2)) print("Horizontally Stacked 1D Arrays:\n", hstacked_array)

Output:

Horizontally Stacked 1D Arrays: [1 2 3 4 5 6]

- Explanation: hstack() joins array1 and array2 side-by-side, creating a longer 1D array.

### Example 4: Horizontal Stacking of 2D Arrays

# Stack two 2D arrays horizontally hstacked_2d = np.hstack((array2d_1, array2d_2)) print("\nHorizontally Stacked 2D Arrays:\n", hstacked_2d)

Output:

Horizontally Stacked 2D Arrays: [[ 1 2 3 7 8 9] [ 4 5 6 10 11 12]]

- Explanation: hstack() adds array2d_2 beside array2d_1, combining them along columns.

## 3. Depth Stacking with dstack()

The dstack() function stacks arrays along a third dimension (depth).

This creates a new depth dimension, making it useful for stacking 2D arrays into 3D arrays.

### Example 5: Depth Stacking of 2D Arrays

# Create two 2D arrays for depth stacking array2d_3 = np.array([[1, 2], [3, 4]]) array2d_4 = np.array([[5, 6], [7, 8]]) # Stack them along a new depth axis dstacked_array = np.dstack((array2d_3, array2d_4)) print("Depth Stacked 2D Arrays:\n", dstacked_array)

Output:

Depth Stacked 2D Arrays: [[[1 5] [2 6]] [[3 7] [4 8]]]

- Explanation: dstack() creates a 3D array by adding a depth dimension, combining array2d_3 and array2d_4 along that axis.

## 4. Stacking Along New Axes with stack()

The stack() function is more versatile, as it allows stacking along any axis by specifying the axis parameter.

It’s particularly useful when you need to stack arrays along new dimensions in higher-dimensional arrays.

### Example 6: Stacking Along a New Axis (axis=0)

# Stack 1D arrays along a new axis stacked_axis0 = np.stack((array1, array2), axis=0) print("Stacked Along New Axis 0:\n", stacked_axis0)

Output:

Stacked Along New Axis 0: [[1 2 3] [4 5 6]]

- Explanation: Stacking along axis=0 creates a 2D array, where array1 and array2 become rows.

### Example 7: Stacking Along a New Axis (axis=1)

# Stack 1D arrays along axis=1 stacked_axis1 = np.stack((array1, array2), axis=1) print("\nStacked Along New Axis 1:\n", stacked_axis1)

Output:

Stacked Along New Axis 1: [[1 4] [2 5] [3 6]]

- Explanation: Stacking along axis=1 creates a 2D array, where each original array is a column.

### Example 8: Stacking 2D Arrays Along a New Depth Axis (axis=2)

# Stack 2D arrays along a new depth axis stacked_2d_axis2 = np.stack((array2d_1, array2d_2), axis=2) print("\nStacked 2D Arrays Along Axis 2:\n", stacked_2d_axis2)

Output:

Stacked 2D Arrays Along Axis 2: [[[ 1 7] [ 2 8] [ 3 9]] [[ 4 10] [ 5 11] [ 6 12]]]

- Explanation: Stacking along axis=2 creates a 3D array by adding a new depth dimension.

## 5. Concatenating vs. Stacking

While concatenate and stack are often used interchangeably, they differ in how they handle dimensions. concatenate joins arrays along existing dimensions, while stack creates a new dimension.

### Example 9: Concatenating vs. Stacking

# Using concatenate along axis 0 (no new dimension) concatenated = np.concatenate((array1[None, :], array2[None, :]), axis=0) print("Concatenated Arrays (No New Dimension):\n", concatenated) # Using stack along axis 0 (adds a new dimension) stacked = np.stack((array1, array2), axis=0) print("\nStacked Arrays (New Dimension):\n", stacked)

Output:

Concatenated Arrays (No New Dimension): [[1 2 3] [4 5 6]] Stacked Arrays (New Dimension): [[1 2 3] [4 5 6]]

- Explanation: concatenate joins without adding a new dimension, while stack adds an extra dimension. In this case, both have the same result, but in higher-dimensional arrays, stack would create a different shape.

## Summary of Stacking Methods in NumPy

Function | Description |
---|---|

vstack() | Vertically stacks arrays along rows (axis=0). |

hstack() | Horizontally stacks arrays along columns (axis=1). |

dstack() | Stacks arrays along a new third dimension (depth-wise). |

stack() | Stacks arrays along any specified axis, adding a new dimension. |

concatenate() | Joins arrays along an existing dimension without adding a new dimension (good for flattening stacks). |

## Conclusion

In this tutorial, we covered various ways to stack arrays in NumPy, including:

- Vertical stacking with vstack().
- Horizontal stacking with hstack().
- Depth stacking with dstack().
- Custom stacking along any axis with stack().

Understanding these stacking techniques is essential for data manipulation, especially in applications involving multi-dimensional data such as image processing, machine learning, and scientific computing.