How to Concatenate Two 2-Dimensional NumPy Arrays?


Python is a versatile and powerful programming language that is widely used in scientific computing, data analysis, and machine learning. One of the key libraries that makes Python so useful for these fields is NumPy. NumPy provides powerful tools for working with arrays, which are essential for many scientific computing tasks. In this article, we will explore how to concatenate two 2−dimensional NumPy arrays using Python.

If you've ever worked with arrays in Python, you know how useful they can be for storing and manipulating large amounts of data. However, you may need to combine two arrays into a single, bigger array. This is where array concatenation comes in. In this tutorial, we will show you how to concatenate two 2−dimensional NumPy arrays in Python using two different methods. So let's get started!

How to Concatenate Two 2−dimensional NumPy Arrays?

Concatenation is the process of combining two or more strings, arrays, or other data structures into a single entity. It involves joining the contents of two or more strings or arrays together to create a new string or array.

There are multiple methods to concatenate a two 2−dimensional NumPy array. Let’s dive into them one by one.

Method 1: Using np.concatenate()

The np.concatenate() function takes a sequence of arrays as its first argument, which could be a tuple, list, or any iterable containing the arrays to be concatenated. We can also specify the axis along which we want to concatenate the arrays, which could be either 0 (for vertical concatenation) or 1 (for horizontal concatenation).

Example

Here is an example of using np.concatenate() to concatenate two 2−dimensional NumPy arrays horizontally:

import numpy as np

# create two 2D arrays
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])

# concatenate horizontally
arr3 = np.concatenate((arr1, arr2), axis=1)

# print the concatenated array
print(arr3)

Output

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

In this above example, we first create two 2D arrays arr1 and arr2 using the np.array() function. Then, we use np.concatenate() to concatenate these arrays horizontally along the second axis (axis=1). The resulting concatenated array arr3 contains all the elements from arr1 and arr2 arranged horizontally. Note that we have specified axis=1 to concatenate the arrays horizontally, and the resulting concatenated array has the same number of rows as the input arrays.

Example

We can also use np.concatenate() to concatenate two 2−dimensional NumPy arrays vertically by specifying axis=0. Here is an example:

import numpy as np

# create two 2D arrays
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])

# concatenate vertically
arr3 = np.concatenate((arr1, arr2), axis=0)

# print the concatenated array
print(arr3)

Output

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

In this above code example, we again created two 2D arrays arr1 and arr2 using the np.array() function, and then use np.concatenate() to concatenate these arrays vertically along the first axis (axis=0). The resulting concatenated array arr3 contains all the elements from arr1 and arr2 arranged vertically. Note that we have specified axis=0 to concatenate the arrays vertically, and the resulting concatenated array has the same number of columns as the input arrays.

Method 2: Using np.vstack() and np.hstack()

In addition to the np.concatenate() function, NumPy provides two other functions that can be used to concatenate 2−dimensional arrays: np.vstack() and np.hstack(). These functions are specifically designed for vertical and horizontal concatenation, respectively.

np.vstack(): This function can be used to stack two 2−dimensional arrays vertically. It takes a tuple of arrays as input and returns a new array with the input arrays stacked vertically. The shape of the resulting array is (m+n, k), where m and n are the number of rows in the input arrays, and k is the number of columns.

Example

Here's an example using np.vstack() method for concatenating two 2−dimensional arrays:

import numpy as np

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

# Stacking the two arrays vertically
result = np.vstack((array1, array2))

print("Array 1:")
print(array1)

print("\nArray 2:")
print(array2)

print("\nResult after vertical concatenation:")
print(result)

Output

Array 1:
[[1 2]
 [3 4]]

Array 2:
[[5 6]
 [7 8]]

Result after vertical concatenation:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

As you can see above, the resulting concatenated array result will have a shape of (4, 2) with the elements of array1 appearing first, followed by the elements of array2. The np.vstack() function stacks arrays vertically, which means that the arrays are placed one on top of the other.

np.hstack(): This function can be used to stack two 2−dimensional arrays horizontally. It takes a tuple of arrays as input and returns a new array with the input arrays stacked horizontally. The shape of the resulting array is (m, n+p), where m is the number of rows in the input arrays and n and p are the numbers of columns in the first and second arrays, respectively.

Example

Here's an example that demonstrates the usage of np.hstack():

import numpy as np

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

# Stacking the two arrays horizontally
result = np.hstack((array1, array2))

print("Array 1:")
print(array1)

print("\nArray 2:")
print(array2)

print("\nResult after horizontal concatenation:")
print(result)

Output

Array 1:
[[1 2]
 [3 4]]

Array 2:
[[5 6]
 [7 8]]

Result after horizontal concatenation:
[[1 2 5 6]
 [3 4 7 8]]

As you can observe in the output, the resulting concatenated array is also a 2−dimensional NumPy array with a shape of (2, 4). The first two columns of the concatenated array contain the elements of 'array1' and the last two columns contain the elements of 'array2'.

Conclusion

In this article, we explored two methods to concatenate two 2−dimensional arrays using Numpy − np.concatenate() and np.vstack()/np.hstack(). We provided examples for each of the methods, demonstrating how to concatenate two 2−dimensional arrays horizontally and vertically using these functions. These methods can be very useful for combining arrays and working with large amounts of data in scientific computing, data analysis, and machine learning tasks.

Updated on: 21-Jul-2023

579 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements