NumPy Joining Arrays (Python Tutorial)

NumPy Joining Arrays

This is a detailed tutorial of the NumPy Joining Arrays. Learn to join multiple NumPy Arrays using the concatenate & stack functions.

As we know we deal with multi-dimensional arrays in NumPy. So in order to combine the content of two arrays into one array, we use this concept of joining.

Usually, we try to join arrays within SQL with the help of keys like Foreign keys and primary keys. But in the case of NumPy, we join the arrays with the help of axes. Also, we have a function in NumPy that helps us in combining the content of two arrays.

Joining Using Concatenate Function

This function is very helpful in joining two completely different arrays into a single unit. And this function is known as concatenate(). It helps in joining two arrays along their axis. And if we do not pass axis then it by default taken as 0. So in order to get the result that we want we need to pass the axis.

Let us take an example which will help us in understanding it in a more beneficial way:

#try importing the numpy package along with creating an alias
import numpy as np
#now we will create array 1
array1=np.array([9,8,7,6])
#now we will create array 2
array2=np.array([1,2,3,4])
#Here we use the function 
array3=np.concatenate((array1,array2))
# here we will print the concatenating arrays
print(array3)

Output.

[9 8 7 6 1 2 3 4]

Here we have taken two arrays then we are joining them by using the function. So we see that it joins two arrays by their axes that is probably their beginning point.

Let us take another example for 2-D arrays:

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array1 
array1=np.array([[9,8],[7,6]]) 
#now we will create array2 
array2=np.array([[1,2],[3,4]]) 
#Here we use the function 
array3=np.concatenate((array1,array2)) 
# here we will print the concatenating arrays 
print(array3)

Output.

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

Here we are taking two-dimensional arrays and trying to join them using the function.

Now we will take another example of a two-dimensional array where we will be defining the axis from where it should join them.

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array1 
array1=np.array([[9,8],[7,6]]) 
#now we will create array2 
array2=np.array([[1,2],[3,4]]) 
#Here we use the function 
array3=np.concatenate((array1,array2), axis=1) 
# here we will print the concatenating arrays 
print(array3)

Output.

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

Here we see that when we are defining the axis form where it should join, then we see a change in the result. So here it is joining form the axis one whereas in the previous example it is taking axis by default as 0.

Joining Using Stack Function

In the process of stacking, we combine two arrays along with their axes, but then there is a new axis that we need in order to join two arrays.

In this concept, we are mainly stacking one array over the other array.

Let us take an example which explains this concept better:

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 1 
array1=np.array([9,8,7,6]) 
#now we will create array 2 
array2=np.array([1,2,3,4]) 
#Here we use the function 
array3=np.stack((array1,array2), axis=1) 
# here we will print the stacking arrays 
print(array3)

Output.

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

Here we are trying to stack along the axis -1.

Row Stacking

In the NumPy package, we have a function that helps us to stack along the row. This function is known as hstack().

Let us take an example to use this function:

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 1 
array1=np.array([9,8,7,6]) 
#now we will create array 2 
array2=np.array([1,2,3,4]) 
#Here we use the function 
array3=np.hstack((array1,array2)) 
# here we will print the arrays 
print(array3)

Output.

[9 8 7 6 1 2 3 4]

Here we are trying to stack along with rows without giving any sort of axis. As a result, we do not need to give any axis to stack along the rows.

Let us take an example in a two-dimensional array:

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array1 
array1=np.array([[9,8],[7,6]]) 
#now we will create array2 
array2=np.array([[1,2],[3,4]]) 
#Here we use the function 
array3=np.hstack((array1,array2)) 
# here we will print the arrays 
print(array3)

Output.

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

Here we are stacking two-dimensional arrays along with the axis to rows.

Column Stacking

In order to stack along with the columns, we have a helper function in NumPy which is known as vstack(). This helps us to use axis along the column in order to stack along with the columns.

Let us go through an example:

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 1 
array1=np.array([9,8,7,6]) 
#now we will create array 2 
array2=np.array([1,2,3,4]) 
#Here we use the function 
array3=np.vstack((array1,array2)) 
# here we will print the arrays 
print(array3)

Output.

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

Here we are stacking up the arrays along the axis of their columns.

Example for 2-D arrays:

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array1 
array1=np.array([[9,8],[7,6]]) 
#now we will create array2 
array2=np.array([[1,2],[3,4]]) 
#Here we use the function 
array3=np.vstack((array1,array2)) 
# here we will print the arrays 
print(array3)

Output.

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

So with this example, we understood that using this helper function, we can stack two arrays along with the column.

Height Stacking

In order to stack along with the depth of the arrays we use dstack(). This is the helper function that will help in stacking along with height, which is at the same depth as height od the array.

Let us go through some examples:

Example 1

#try importing the numpy package along with creating an alias 
import numpy as np #now we will create array 1 
array1=np.array([9,8,7,6]) 
#now we will create array 2 
array2=np.array([1,2,3,4]) 
#Here we use the function 
array3=np.dstack((array1,array2)) 
# here we will print the arrays 
print(array3)

Output.

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

Example 2

#try importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array1 
array1=np.array([[9,8],[7,6]]) 
#now we will create array2 
array2=np.array([[1,2],[3,4]]) 
#Here we use the function 
array3=np.dstack((array1,array2)) 
# here we will print the arrays 
print(array3)

Output.

[[[9 1]
  [8 2]]

 [[7 3]
  [6 4]]]

So here we see the stacking of arrays as per their depth. So the axis of the stacking is the height of the arrays which are supposed to be joined. As a result, we see that the arrays are stacked up, but the height of the array is the same as the depth of the array.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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