How to merge all the arrays into a single array with Numpy

In this tutorial, we will learn about how multiple arrays can be merged into a single array in Python using Numpy. Merging simply means that the contents of two or more arrays are combined into a single array.

In Numpy, arrays are merged on the basis of axes. Axes are also known as “dimensions.” Similar to the mathematical concept of dimensions, array “axes” or dimensions describe the direction along which elements of an array are arranged.

In a 1-dimensional array, there is only one axis. In a 2-dimensional array (a matrix), there are two axes: axis 0 and axis 1. Axis 0 corresponds to the rows of the matrix, and axis 1 corresponds to the columns. This concept is especially relevant to us when talking about merging two or more arrays since we can now define merging on the basis of columns or rows for 2D arrays.

Let’s understand how exactly we can accomplish merging of arrays in Numpy with the following methods:

  • Using the ‘concatenate()’ function
  • Using ‘stack()’ function and its variants

Merging arrays into a single array in Numpy

Using the ‘concatenate()’ function:

The concatenate() function takes two arguments: the arrays to be merged and the axis along which to be merged. With this function, two or more arrays can be merged along a user-specified axis.

import numpy as np

array1 = np.array([4, 5, 6])
array2 = np.array([7, 8, 9])
array3 = np.array([10, 11 ,12])
new = np.concatenate((array1, array2, array3), axis = 0)
print(new)

Output:

[ 4 5 6 7 8 9 10 11 12]

Above, we define 3 1D arrays and use the concatenate() function to merge them into a single 1D array called ‘new.’ Observe that axis is defined to be 0. This means that the arrays are merged row-wise (horizontal axis).

Let’s now take the example of 2-D arrays, i.e., arrays within an array. How does the concatenate() function operate for these arrays?

import numpy as np

array1 = np.array([[4, 5, 6], [7, 8, 9]])
array2 = np.array([[10, 11, 12], [13, 14, 15]])
new = np.concatenate((array1, array2), axis = 0)
print(new)

Output:

[[ 4 5 6]
[ 7 8 9]
[10 11 12]
[13 14 15]]

We have now used two 2-D arrays and passed them within concatenate() function. By specifying the axis argument to be equal to 0, we have merged the arrays into a 2D array called ‘new’ in a row-wise fashion. Now, what if we change the axis argument’s value?

import numpy as np

array1 = np.array([[4, 5, 6], [7, 8, 9]])
array2 = np.array([[10, 11, 12], [13, 14, 15]])
new = np.concatenate((array1, array2), axis = 1)
print(new)

Output:

[[ 4 5 6 10 11 12]
[ 7 8 9 13 14 15]]

Clearly, the output of the code has changed when we change axis argument value to value 1. By changing the axis value, we have performed column-wise concatenation of the 2 input 2-D arrays using the concatenate() function.

Using the ‘stack()’ function:

stack() is also used as a joining function. However, this function joins the arrays on a new axis or dimension. This is unlike the functionality of the concatenate() function.

It takes 3 arguments: the arrays, user-defined axis value for arrays to be stacked, and an ‘out’ value which represents the resting point of the output array.

Let’s observe how this function works.

import numpy as np

array1 = np.array([1, 2, 3, 4])
array2 = np.array([5, 6, 7, 8])
new = np.stack((array1, array2), axis = 0)
print(new)

Output:

[[1 2 3 4]
[5 6 7 8]]

The above code takes 2 input 1D arrays and returns a 2D array called ‘new.’ Both input arrays are stacked row-wise since the axis is defined to be 0.

import numpy as np

array1 = np.array([1, 2, 3, 4])
array2 = np.array([5, 6, 7, 8])
new = np.stack((array1, array2), axis = 1)
print(new)

Output:

[[1 5]
[2 6]
[3 7]
[4 8]]

Observe that the output 2-D array is now stacked column-wise. This occurs when the axis argument is given the value 1 instead of 0. How would this look like with 2D arrays as inputs?

import numpy as np 

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

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

Output:

[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]

The function now outputs a 3D array for 2D array inputs. Additionally, specifying axis = 0 stacks the arrays sequentially.

However, changing the axis value changes the output.
For axis =1,

Output:

[[[ 1 2 3]
[ 7 8 9]]

[[ 4 5 6]
[10 11 12]]]

We observe a 3-D array as output with row-wise stacking of the input 2D arrays.

For axis = 2,

Output:

[[[ 1 7]
[ 2 8]
[ 3 9]]

[[ 4 10]
[ 5 11]
[ 6 12]]]

With axis = 2, a 3D array is obtained as output. However, python stacks the input 2D arrays in a column-wise fashion.

Let’s now understand the other types of stack functions.

‘vstack()’ function:

The vstack() function takes 1 argument: the arrays to be merged. It then outputs an array with the input arrays arranged vertically.

import numpy as np 

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

new = np.vstack((array1, array2))
print(new) 

Output:

[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

A 2D array is returned with input 2D arrays stacked one on top of each other.

‘hstack()’ function:

The hstack() function takes 1 argument: the arrays to be merged. It then outputs an array with the input arrays arranged horizontally.

import numpy as np 

array1 = np.array([[1, 2], [3, 4]]) 
array2 = np.array([[5, 6], [7, 8]]) 

new = np.hstack((array1, array2))
print(new)

Output:

[[1 2 5 6]
[3 4 7 8]]

Observe that the 2 input 2D arrays are stacked horizontally element-wise and stored in a 2D array called ‘new.’

‘dstack()’ function:

The dstack() function takes 1 argument: the arrays to be merged. It then outputs an array with the input arrays merged on the basis of equal index by index values.

import numpy as np 

array1 = np.array([1, 2, 3]) 
array2 = np.array([5, 6, 7]) 

new = np.dstack((array1, array2))
print(new) 

Output:

[[[1 5]
[2 6]
[3 7]]]

The function takes 2 1D arrays as input and returns a 3D array with each array’s elements stacked index value-wise within the output array.

As you can see, there are multiple methods of merging two or more arrays in Python into a single array using Numpy! A programmer can choose the method that best suits the coding task.

Thank you for reading this tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top