Python

Concatenate Numpy Arrays

The term “joining” refers to the process of combining the elements of two or maybe more arrays into a single array. Concatenation also is referred to as joining. In SQL, tables are joined by a key, but in NumPy, arrays are joined by an axis. The concatenate() function of python Numpy is specially used for this purpose. The method joins two or more similar-shaped arrays across a given axis. The arguments for the method are the pair of arrays and the axis. The axis is sent to the concatenate() method together with a series of arrays that we wish to connect. If the axis isn’t expressly specified, it defaults set to 0. Let’s see how the to concatenate function works in python to join two arrays within Spyder 3 of Windows 10. Let’s take a look at some examples.

Example 01:

Let’s look at our first example of concatenating two NumPy arrays within python using the Spyder 3 tool. To use NumPy arrays, we must import the NumPy package as object “n” using the keyword “import”. After this, the NumPy “array()” function has been used to define two arrays of integer type and save them to the variables A1 and A2 separately. The NumPy object “n” has been used for this purpose so far. We have been utilizing the NumPy’s concatenate() function here to join both the NumPy arrays A1 and A2 together, and a newly formed array has been saved to the variable “A”. The arrays have been used as a single argument within simple brackets. Both the NumPy arrays A1 and A2 have been displayed on the Spyder 3 console while utilizing the print function of python. We output the concatenated new array A on the Spyder 3 screen in the last print method.

import numpy as n

A1 = n.array([1, 3, 5, 7, 9])

A2 = n.array([2, 4, 6, 8, 10])

A = n.concatenate((A1, A2))

print("Array 1:", A1)

print("Array 2:", A2)

print("Concatenated Array:", A)

Let’s save and execute this code to see the results. So, the output shows both the arrays separately at the output screen and then the concatenated array as demonstrated. Within this example code, as we didn’t define the axis argument, thus it is taking an axis as zero. Due to this, the array has been defined in a single row with no further axis.

Example 02:

Let’s take a look at one step forward while using the concatenate function of NumPy. So, the Numpy package has been imported first. This time, we initialized two NumPy arrays, each with two elements as lists separated by a comma. The concatenation has been performed using both arrays in the concatenate function. We have also used the axis argument set to None here. This will make a single-line array. The newly made array has been saved in variable A. The original single arrays have been displayed on the python console via the print function. After that, the concatenated array A was printed out with the print method.

import numpy as n

A1 = n.array([[1, 2], [3, 4]])

A2 = n.array([[5, 6], [7, 8]])

A = n.concatenate((A1, A2), axis=None)

print("Array 1:\n", A1)

print("Array 2:\n", A2)

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

After running this code, we have got the single arrays first and then the concatenated single line array in the form of a list.

Let’s update the code a little. So, we have been changing the axis value of the concatenate() function in the code. While the rest of the code has been the same and unchanged. We have replaced the axis value from None to 0. It will surely make the concatenated string with 0 axis, i.e. all the values will be displayed as it is without any change and separately.

import numpy as n

A1 = n.array([[1, 2], [3, 4]])

A2 = n.array([[5, 6], [7, 8]])

A = n.concatenate((A1, A2), axis=0)

print("Array 1:\n", A1)

print("Array 2:\n", A2)

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

After executing the updated code, we have got the result below. The array items have been defined separately in the concatenated array without merging but displayed as a single array here.

Let’s update the code by changing the value of the axis to 1 in the concatenate() function.

import numpy as n

A1 = n.array([[1, 2], [3, 4]])

A2 = n.array([[5, 6], [7, 8]])

A = n.concatenate((A1, A2), axis=1)

print("Array 1:\n", A1)

print("Array 2:\n", A2)

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

After running his code, we have got both arrays separately and the concatenated array as x and y-axis in the same line.

Example 04:

The same functionality can be performed by the python’s stack function in the code. So we have been using our last example to see if it works the same as the concatenate() function does. So, the simple change is the replacement of the “concatenate()” method with the “stack” function here. Let’s save our code to make it executed with the SPyder’s run button.

import numpy as n

A1 = n.array([1, 3, 5, 7, 9])

A2 = n.array([2, 4, 6, 8, 10])

A = n.stack((A1, A2))

print("Array 1:", A1)

print("Array 2:", A2)

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

After the code execution of the stack() function in Python, we have got the concatenated array in an axis equal to 1.

Conclusion:

We have done all the demonstrations and examples of using the concatenate() function of python using the NumPy library. We have used it to concatenate NumPy arrays. We have also discussed using the axis argument while set to None, 0, and 1. Also, we have added the bonus example to see the working of stack function as an alternative to concatenate() method. We are hoping high for this article as it contains a simple and elegant way to explain each and everything briefly.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content