NumPy: How to concatenate arrays vertically and horizontally

Updated: January 22, 2024 By: Guest Contributor Post a comment

Overview

NumPy is one of the most fundamental libraries for scientific computing in Python. It provides powerful capabilities to create and manipulate numerical data through its robust array objects. One of the essential operations when working with NumPy arrays is concatenation – combining arrays to form larger ones. This tutorial will guide you through various ways to concatenate arrays both vertically and horizontally using the NumPy library, complete with code examples and expected outputs.

Getting Started with NumPy

First, ensure that you have NumPy installed. If you haven’t installed NumPy yet, you can do it using pip by running the following command:

pip install numpy

With NumPy installed, you can import it and begin working with arrays:

import numpy as np

Understanding Array Shapes

Before we dive into concatenation, it’s important to understand the shape of NumPy arrays. The shape determines how we can combine arrays. An array’s shape is a tuple indicating the size along each dimension. For example:

a = np.array([[1, 2], [3, 4]])
print(a.shape)  # Output: (2, 2)

Array a has a shape of (2, 2), meaning it is a 2×2 matrix.

Horizontal Concatenation

To concatenate two or more arrays horizontally, you can use the np.concatenate function with the axis=1 parameter, or the np.hstack (horizontal stack) shortcut. Both arrays must have the same number of rows.

# Create two arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# Concatenate horizontally
h_concat = np.concatenate((a, b), axis=1)
print(h_concat)
# Output: [[1 2 5 6]
#         [3 4 7 8]]

# Using hstack
h_stack = np.hstack((a, b))
print(h_stack)
# Output: [[1 2 5 6]
#         [3 4 7 8]]

Vertical Concatenation

For vertical concatenation, you can utilize np.concatenate with axis=0 or use the np.vstack (vertical stack) function. In this case, arrays must have the same number of columns.

# Create two arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# Concatenate vertically
v_concat = np.concatenate((a, b), axis=0)
print(v_concat)
# Output: [[1 2]
#         [3 4]
#         [5 6]
#         [7 8]]

# Using vstack
v_stack = np.vstack((a, b))
print(v_stack)
# Output: [[1 2]
#         [3 4]
#         [5 6]
#         [7 8]]

Concatenation with Unequal Dimensions

When you need to concatenate arrays with different dimensions, the np.pad function helps by padding arrays to make their dimensions compatible.

Let’s take the following arrays:

c = np.array([[1], [2]])
d = np.array([[3, 4]])

To horizontally concatenate c and d, we need make their number of rows equal:

c_padded = np.pad(c, ((0, 1), (0, 0)), mode='constant', constant_values=0)
print(c_padded)
# Output: [[1]
#         [2]
#         [0]]

h_concat_diff = np.hstack((c_padded, d))
print(h_concat_diff)
# Output: [[1 3 4]
#         [2 0 0]
#         [0 0 0]]

Similarly, use np.pad for the vertical concatenation of arrays with different numbers of columns.

Advanced Concatenation Using np.r_ and np.c_

NumPy also provides the np.r_ and np.c_ indexer objects for stacking arrays along rows (axis=0) and columns (axis=1), respectively. These are useful for quickly building up arrays without creating them manually first.

e = np.r_[a, b]
print(e)
# Output: [[1 2]
#         [3 4]
#         [5 6]
#         [7 8]]

f = np.c_[a, b]
print(f)
# Output: [[1 2 5 6]
#         [3 4 7 8]]

Conclusion

Concatenating arrays is a frequently used operation when handling data in NumPy, and as we have seen, this can be achieved horizontally or vertically using multiple functions tailored to different requirements. Understanding and mastering array concatenation will undoubtedly enhance your data manipulation capabilities within Python’s NumPy library.