NumPy Joining Arrays

NumPy Joining Arrays

NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for scientific computing, which contains a collection of tools and techniques that can be used to solve mathematical models. The most widely used array-processing library in Python is Numpy which provides the ultimate convenience while working with array objects, that allow manipulating large mathematical datasets.

One of the main features of NumPy is the ability to manipulate arrays quickly and efficiently. In this tutorial, we will be discussing how to join two or more arrays into a single array. NumPy provides various functions to join arrays.

Following are the topics which we will discuss in this tutorial –

  • Joining Concatenate
  • Joining with stacks
  • Join Different Shapes
  • Joining Units Together

Joining Concatenate

The concatenate() function is used to concatenate two or more arrays. We can use the concatenate function to combine two or more arrays row-wise or column-wise.

To use the concatenation function, we should have two or more arrays and pass all of these arrays as the tuple argument in the concatenate function.

Example:

import numpy as np

# Creating two arrays to join
array_1 = np.array([[1, 2], [3, 4]])
array_2 = np.array([[5, 6]])

# Joining arrays vertically
joined_array = np.concatenate((array_1, array_2.T), axis=1)

# Printing the joined array
print(joined_array)

The output of the above code will be:

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

In the above code, we have created two arrays ‘array_1’ and ‘array_2’ of shapes (2, 2) and (1, 2) respectively. We have joined these arrays vertically into a single array by passing these arrays as a tuple argument into the concatenate function. We have set the axis parameter to 1. Therefore, arrays are joined column-wise.

Joining with stacks

We can also join arrays vertically with the help of ‘vertical stack’ function. The vstack() function is used to join two or more arrays vertically i.e. on top of each other. To join arrays with the vstack function, we should pass all of these arrays as the tuple argument in the vstack function.

Example:

import numpy as np
  
# Creating two arrays to join
array_1 = np.array([[1, 2], [3, 4]])
array_2 = np.array([[5, 6]])

# Joining arrays vertically 
joined_array = np.vstack((array_1, array_2))

# Printing the joined array
print(joined_array)

The output of the above code will be:

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

In the above code, we have created two arrays ‘array_1’ and ‘array_2’ of shapes (2, 2) and (1, 2) respectively. We have joined these arrays vertically into a single array by passing these arrays as a tuple argument into the vstack function.

Join Different Shapes

If we try to join two arrays having different shapes, the NumPy concatenate function will generate a “ValueError”. However, NumPy provides other join functions which can join arrays with different shapes as well.

To join arrays with different shapes, the hstack() function is used. This function stacks arrays horizontally i.e. by column-wise.

Example:

import numpy as np

# Creating two arrays to join
array_1 = np.array([1, 2, 3])
array_2 = np.array([4, 5, 6])

# Joining arrays horixontally 
joined_array = np.hstack((array_1, array_2))

# Printing the joined array
print(joined_array)

The output of the above code will be:

[1 2 3 4 5 6]

In the above code, we have created two arrays ‘array_1’ and ‘array_2’ of shapes (3,) and (3,) respectively. We have joined these arrays horizontally into a single array by passing these arrays as a tuple argument into the hstack function.

Joining Units Together

The column_stack() function provides a way to stack 1-D arrays as columns to create a 2-D array. We can also join 2D arrays horizontally (column-wise) with the column_stack function.

Example:

import numpy as np
  
# Creating two arrays to join
array_1 = np.array([[1, 2], [3, 4]])
array_2 = np.array([[5, 6], [7, 8]])

# Joining arrays horizontally
joined_array = np.column_stack((array_1, array_2))

# Printing the joined array
print(joined_array)

The output of the above code will be:

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

In the above code, we have created two arrays ‘array_1’ and ‘array_2’ of shapes (2, 2) and (2, 2) respectively. We have joined these arrays horizontally into a single array by passing these arrays as a tuple argument into the column_stack function.

Conclusion

Numpy provides numerous functions for joining arrays. We can join two or more arrays vertically or horizontally. We can also join arrays of different shapes with the help of numpy’s join functions.

Leave a Reply

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