Yandex

Course IndexCourse Index0

    ProgramGuru

    Stacking Arrays in NumPy
    Horizontal, Vertical, and Depth


    Introduction to Stacking in NumPy

    In real-world data processing, combining arrays is often a necessity. Whether you're appending features, merging images, or just reorganizing data — stacking in NumPy is your go-to operation. Unlike concatenation which merges arrays along an existing axis, stacking creates a new one. Let's unravel how this works.

    Why Learn Stacking?

    Stacking provides a clean, readable, and efficient way to combine multiple arrays. It’s especially helpful when working with matrices, image channels, or multi-dimensional datasets.

    Basic Setup

    import numpy as np
    
    a = np.array([1, 2, 3])
    b = np.array([4, 5, 6])

    Now that we have two simple 1D arrays, let’s start stacking.

    1. Vertical Stacking: np.vstack

    Stacks arrays in sequence vertically (row wise). A new row is created for each array.

    np.vstack((a, b))
    array([[1, 2, 3],
           [4, 5, 6]])

    This creates a 2D array with 2 rows and 3 columns.

    2. Horizontal Stacking: np.hstack

    Stacks arrays in sequence horizontally (column wise).

    np.hstack((a, b))
    array([1, 2, 3, 4, 5, 6])

    Since a and b are both 1D arrays, the result is still 1D — just longer.

    3. Depth Stacking: np.dstack

    Stacks arrays along the third dimension (depth).

    np.dstack((a, b))
    array([[[1, 4],
            [2, 5],
            [3, 6]]])

    Notice the triple brackets — it’s a 3D array with shape (1, 3, 2). Think of this like stacking colored image channels together (Red, Green, Blue).

    4. General Stacking: np.stack

    With np.stack, you can specify exactly which axis you want to stack on.

    np.stack((a, b), axis=0)
    array([[1, 2, 3],
           [4, 5, 6]])
    np.stack((a, b), axis=1)
    array([[1, 4],
           [2, 5],
           [3, 6]])

    Axis 0 stacks them as rows, axis 1 stacks them as columns. The flexibility here is incredibly powerful when reshaping complex datasets.

    What to Watch Out For

    • All arrays must have the same shape along all axes except the one you’re stacking on.
    • Mixing dimensions (e.g., stacking 1D with 2D) throws an error.
    • Use np.newaxis to reshape arrays where needed before stacking.

    Verification Example

    a.shape  # (3,)
    b.shape  # (3,)
    np.vstack((a, b)).shape     # (2, 3)
    np.hstack((a, b)).shape     # (6,)
    np.dstack((a, b)).shape     # (1, 3, 2)

    When Should You Use Stacking?

    If you want to:

    • Build 2D arrays from multiple 1D vectors — use vstack or stack.
    • Combine features side-by-side — use hstack.
    • Create channels or batch inputs — use dstack.

    Conclusion

    Stacking is one of those fundamental array operations that unlocks a whole new world of possibilities. Whether you’re assembling datasets, feeding models, or manipulating matrices — mastering stacking ensures your data is always in the right shape.

    Next Step

    In the next tutorial, we’ll look at splitting arrays and how to reverse the stacking process smartly.



    Welcome to ProgramGuru

    Sign up to start your journey with us

    Support ProgramGuru.org

    You can support this website with a contribution of your choice.

    When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

    PayPal

    UPI

    PhonePe QR

    MALLIKARJUNA M