Yandex

Course IndexCourse Index0

    ProgramGuru

    NumPy Array Concatenation
    Combine Arrays


    Introduction

    In data processing and scientific computing, you often need to combine multiple arrays into one. This is known as concatenation. NumPy provides powerful and flexible tools to perform concatenation along various axes, whether you're stacking rows, merging columns, or creating new dimensional combinations.

    What is Array Concatenation?

    Concatenation in NumPy refers to the operation of joining two or more arrays along an existing axis. It's like taking two blocks and placing them side-by-side or one on top of the other — depending on the direction you choose to merge them.

    Common Methods for Concatenation

    NumPy offers several ways to concatenate arrays:

    • np.concatenate() – General-purpose concatenation along any axis
    • np.vstack() – Vertical stack (along rows)
    • np.hstack() – Horizontal stack (along columns)

    Using np.concatenate()

    The np.concatenate() function is the most versatile method. You can join arrays along any axis, provided their dimensions match except in the concatenating axis.

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

    Explanation:

    We’re combining a and b vertically. The number of columns must match (which is 2 in both), and we are stacking along axis 0 (rows).

    Using np.vstack()

    This is a convenience method for stacking vertically (along axis=0).

    a = np.array([1, 2])
    b = np.array([3, 4])
    result = np.vstack((a, b))
    print(result)
    [[1 2]
     [3 4]]

    Think of vstack as placing arrays on top of one another. Each array must have the same number of columns (or shape[1]).

    Using np.hstack()

    Horizontal stacking places arrays side by side (along axis=1).

    a = np.array([[1], [2]])
    b = np.array([[3], [4]])
    result = np.hstack((a, b))
    print(result)
    [[1 3]
     [2 4]]

    Explanation:

    Both arrays have two rows, so NumPy aligns them row-wise and merges their columns. The result is a 2D array with more columns.

    Important Checks Before Concatenation

    • Ensure shapes are compatible. For axis 0, the number of columns must match; for axis 1, the number of rows must match.
    • All arrays should have the same number of dimensions.
    • Use np.expand_dims() to fix shape mismatches when needed.

    When Things Go Wrong

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

    Error:

    ValueError: all the input arrays must have same number of dimensions

    This is a common mistake. a is 2D while b is 1D. To fix it, reshape b to match the dimensionality:

    b = b.reshape(1, 2)  # Now b is 2D
    np.concatenate((a, b), axis=0)

    Best Practices

    • Use np.concatenate() for maximum control.
    • Use hstack and vstack for readability when stacking in obvious directions.
    • Check array.shape before concatenation — mismatches are the #1 cause of errors.

    Conclusion

    Concatenation is a foundational skill in NumPy. Whether you're merging datasets or preparing input for machine learning models, knowing how to stack and align arrays unlocks greater flexibility in your code. Start with concatenate() for fine control, and switch to hstack or vstack for quick, readable operations.



    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