NumPy - Joining Arrays



Joining Arrays in NumPy

Joining arrays in NumPy refers to the process of combining two or more arrays into a single array. The result may vary depending on the dimensions and axes along which the arrays are joined.

NumPy provides several functions for joining of arrays along different axes, they are −

  • The np.concatenate() Function
  • The np.stack() Function
  • The np.hstack() Function
  • The np.vstack() Function

Using concatenate() Function

The NumPy concatenate() function joins a sequence of arrays along a specified axis. The arrays must have the same shape, except in the dimension corresponding to the axis along which they are being concatenated. Following is the basic syntax −

np.concatenate((array1, array2, ...), axis=0)

Where,

  • array1, array2, ... − It is the sequence of arrays to be concatenated. These arrays should have the same shape along all axes except for the one specified by axis.
  • axis − It is the axis along which the arrays will be joined. The default value is 0, meaning the arrays will be concatenated along rows (for 2D arrays).

Example: Concatenating 1D Arrays

In the following example, we are concatenating two 1D arrays, "array1" and "array2" along the default axis (axis 0) −

import numpy as np

# Create two 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Concatenate along the default axis (axis=0)
result = np.concatenate((array1, array2))

print("Concatenated Array:", result)

The resulting array is a single 1D array containing all elements from both input arrays −

Concatenated Array: [1 2 3 4 5 6]

Example: Concatenating 2D Arrays Along Different Axes

Here, we are concatenating two 2D arrays along different axes in NumPy using the concatenate() function −

import numpy as np

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

# Concatenate along axis 0 (row-wise)
result_axis_0 = np.concatenate((array1, array2), axis=0)

print("Concatenated along Axis 0:\n", result_axis_0)

# Concatenate along axis 1 (column-wise)
result_axis_1 = np.concatenate((array1, array2), axis=1)

print("Concatenated along Axis 1:\n", result_axis_1)

Following is the output obtained −

Concatenated along Axis 0:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
Concatenated along Axis 1:
 [[1 2 5 6]
 [3 4 7 8]]

Example: Concatenating Arrays of Different Dimensions

In here, we reshape the 1D array "array1" into a 2D array with the given shape, making it compatible for concatenation with the 2D array "array2" along axis "0" −

import numpy as np

# Create a 1D and a 2D array
array1 = np.array([1, 2, 3])
array2 = np.array([[4, 5, 6], [7, 8, 9]])

# Reshape array1 to make it 2D for concatenation along axis 0
array1_reshaped = array1.reshape(1, -1)

# Concatenate along axis 0
result = np.concatenate((array1_reshaped, array2), axis=0)

print("Concatenated Array:\n", result)

This will produce the following result −

Concatenated Array:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Stacking Arrays Using stack() Function

The NumPy stack() function joins a sequence of arrays along a new axis, which you specify. This function is useful when you need to preserve the original dimensions of the arrays while adding a new axis for stacking. Following is the syntax −

np.stack((array1, array2, ...), axis=0)

Where,

  • array1, array2, ... − It is the sequence of arrays to be stacked. These arrays must have the same shape.
  • axis − It is the axis along which the arrays will be stacked. The default value is 0, which means the arrays are stacked along a new first axis.

Example: Stacking 2D Arrays

In the following example, we are stacking two 2D arrays along a new third axis. This creates a 3D array with a particular shape −

import numpy as np

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

# Stack along a new third axis (axis 2)
stacked_array = np.stack((array1, array2), axis=2)

print("Stacked Array along Axis 2:\n", stacked_array)
print("Shape of Stacked Array:", stacked_array.shape)

The new dimension represents the depth of the array, where corresponding elements from the original arrays are stacked together −

Stacked Array along Axis 2:
[[[1 5]
  [2 6]]

 [[3 7]
  [4 8]]]
Shape of Stacked Array: (2, 2, 2)

Example: Stacking Multiple Arrays

In this example, three 1D arrays are stacked along a new first axis using the stack() function −

import numpy as np

# Create three 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
array3 = np.array([7, 8, 9])

# Stack along the first axis (new axis 0)
stacked_array = np.stack((array1, array2, array3), axis=0)

print("Stacked Array:\n", stacked_array)
print("Shape of Stacked Array:", stacked_array.shape)

The result is a 2D array where each original array forms a row in the new array −

Stacked Array:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Shape of Stacked Array: (3, 3)

Horizontal Stacking Using hstack() Function

The NumPy hstack() function stacks arrays in sequence horizontally (column-wise). This means that the arrays are concatenated along their second dimension (axis 1 for 2D arrays). For 1D arrays, they are simply concatenated along the single available axis.

Following is the syntax −

np.hstack((array1, array2, ...))

Where, array1, array2, ... is a sequence of arrays that you want to stack horizontally. All arrays must have the same shape along all but the second axis.

Example

In the example below, we are stacking two 2D arrays, "array1" and "array2" horizontally, resulting in a new array where the columns of "array2" are placed to the right of the columns of "array1" −

import numpy as np

# Create two 2D arrays
array1 = np.array([[1, 2, 3],
                   [4, 5, 6]])
array2 = np.array([[7, 8, 9],
                   [10, 11, 12]])

# Horizontally stack the arrays
hstacked_array = np.hstack((array1, array2))

print("Horizontally Stacked 2D Array:\n", hstacked_array)

Following is the output of the above code −

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

Vertical Stacking Using vstack() Function

The NumPy vstack() function stacks arrays in sequence vertically (row-wise). This means that the arrays are concatenated along their first dimension (axis 0 for 2D arrays). For 1D arrays, they are treated as rows and stacked on top of each other, resulting in a 2D array.

Following is the syntax −

np.vstack((array1, array2, ...))

Where, array1, array2, ... is a sequence of arrays that you want to stack vertically. All arrays must have the same shape along all but the first axis.

Example

In the following example, we are stacking two 2D arrays, "array1" and "array2" vertically, resulting in a new array where the rows of "array2" are placed below the rows of "array1" −

import numpy as np

# Create two 2D arrays
array1 = np.array([[1, 2, 3],
                   [4, 5, 6]])
array2 = np.array([[7, 8, 9],
                   [10, 11, 12]])

# Vertically stack the arrays
vstacked_array = np.vstack((array1, array2))

print("Vertically Stacked 2D Array:\n", vstacked_array)

The output obtained is as shown below −

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

Splitting Arrays After Joining

After joining arrays, you might want to split them back into their original or differently shaped parts. NumPy provides several functions for splitting arrays −

  • np.split() function − Splits an array into multiple sub-arrays as specified by the indices or sections.
  • np.array_split() function − Similar to np.split() function, but allows splitting into unequal sub-arrays.
  • np.hsplit() function − Splits an array horizontally (column-wise).
  • np.vsplit() function − Splits an array vertically (row-wise).
  • np.dsplit() function − Splits an array along the third axis (depth-wise), used for 3D arrays.
Array splitting is the process of dividing an array into multiple sub-arrays. This operation is the inverse of array joining.

Example

In the following example, we are splitting the vertically stacked array into two equal parts using the np.vsplit() function, effectively reversing the vertical stacking operation −

import numpy as np

# Vertically stack two arrays
array1 = np.array([[1, 2, 3],
                   [4, 5, 6]])
array2 = np.array([[7, 8, 9],
                   [10, 11, 12]])

vstacked_array = np.vstack((array1, array2))

# Split the array back into the original arrays
split_arrays = np.vsplit(vstacked_array, 2)

print("Split Arrays:")
for arr in split_arrays:
   print(arr)

After executing the above code, we get the following output −

Split Arrays:
[[1 2 3]
 [4 5 6]]
[[ 7  8  9]
 [10 11 12]]
Advertisements