Python NumPy hstack Function

NumPy hstack() function in Python is used to stack or concatenate arrays in sequence horizontally (column-wise). This process is similar to concatenate arrays along the second axis, except for 1-D arrays where it concatenates along the first axis. It stacks a sequence of two or more arrays into a single NumPy array.

In this article, I will explain numpy.hstack() function syntax and how to create an array that is formed by stacking the given arrays horizontally.

1. Quick Examples of hstack() Function

Following are quick examples of the hstack() function in Python. For more functions of NumPy refer to NumPy Tutorial.


# Below are the quick examples

# Example 1: Use NumPy.hstack() Functions
arr = np.array([ 2, 3, 4])
arr1 = np.array([5, 6, 7])
arr2 = np.hstack((arr,arr1))

# Example 2: Use numpy.hstack() function to 2-d numpy arrays
arr =  np.array([[ 2, 3, 5], [ -1, -3, -5]])
arr1 = np.array([[ 6, 8, 10], [ -7, -8, -9]])
arr2 = np.hstack((arr,arr1))

# Example 3: stacking arrays horizontally
arr =  np.array([[ 2, 3], [ 4, 6]])
arr1 = np.array([[ 6, 8, 10], [ 8, 10, 12]])
arr2 = np.hstack((arr,arr1))

2. Syntax of NumPy hstack()

Following is the Syntax of hstack().


# Syntax of NumPy hstack()
numpy.hstack(tup)

2.1 Parameters of hstack()

Following are the Parameters hstack()

  • tup – It provides the sequence of arrays to be concatenated.

2.2 Return Value of hstack()

It returns an array formed by stacking over the given arrays horizontally.

3. Use numpy.hstack() Function

Use numpy.hstack() function to concatenate arrays horizontally (column wise). When you use hstack() on multiple arrays of 1-D, it combines all arrays and returns the result in a single array.

Let’s create two 1-dimensional arrays of length three and then horizontally stacked them with the hstack() function.


import numpy as np

# Create NumPy arrays 
arr = np.array([ 2, 3, 4])
arr1 = np.array([5, 6, 7])

# Use NumPy.hstack() Functions
arr2 = np.hstack((arr,arr1))
print(arr2)

# Output
# [2 3 4 5 6 7]

4. Use hstack() Function to 2-D Numpy Arrays

To stack() the 2-D arrays using hstack() function, it will return the horizontally stacked array. Here we stacked two 2d arrays of shape (2,3) horizontally resulting in an array of shape (2,6).


# creating an 2D input array
arr =  np.array([[ 2, 3, 5], [ -1, -3, -5]])
arr1 = np.array([[ 6, 8, 10], [ -7, -8, -9]])

# Use numpy.hstack() function to 2-d numpy arrays
arr2 = np.hstack((arr,arr1))
print(arr2)

# Output
# [[ 2  3  5  6  8 10]
#  [-1 -3 -5 -7 -8 -9]]

Below is another example where two arrays have the same shape along all except different second axis.


# creating an 2D input array
arr =  np.array([[ 2, 3], [ 4, 6]])
arr1 = np.array([[ 6, 8, 10], [ 8, 10, 12]])

# stacking arrays horizontally
arr2 = np.hstack((arr,arr1))
print(arr2)

# Output
# [[ 2  3  6  8 10]
# [ 4  6  8 10 12]]

5. Conclusion

In this article, I have explained numpy.hstack() function and using this function how we can stack the NumPy arrays horizontally.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing Python NumPy hstack Function