• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing Python NumPy hstack Function

The numpy.hstack() function in Python’s NumPy library is used to horizontally(column-wise) stack arrays. It concatenates the input arrays along the second axis (axis=1), i.e., it joins them horizontally. 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.

Advertisements

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

1. Quick Examples of hstack() Function

If you are in a hurry, below are some quick examples of the NumPy hstack() function in Python. For more functions of NumPy refer to NumPy Tutorial.


# Quick examples of hstack() function

# 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 is a sequence of input arrays that you want to stack horizontally. This can be a tuple, list, or any iterable containing arrays.

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 stack them with the hstack() function. The numpy.hstack() function is used to horizontally stack arrays.

In the below example, arr and arr1 are two 1-dimensional arrays. The numpy.hstack() function is used to horizontally stack them, resulting in a new 1-dimensional array arr2 with elements from both arr and arr1.


# Import numpy
import numpy as np

# Create 2-D array
arr = np.array([ 2, 3, 4])
print("First array:\n",arr)
arr1 = np.array([5, 6, 7])
print("Second array:\n",arr1)

# Use NumPy.hstack() functions
arr2 = np.hstack((arr,arr1))
print("Horizontally stacked array:\n",arr2) 

Yields below output.

numpy hstack

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

When working with 2-dimensional NumPy arrays, the numpy.hstack() function can be used to horizontally stack arrays along the second axis (columns).

In the below example, arr and arr1 are two 2-dimensional arrays. The numpy.hstack() function is used to horizontally stack them, resulting in a new 2-dimensional array arr2 with columns from both arr and arr1. The resulting array has the shape (2, 6) because it concatenates along the columns.


# 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("Horizontally stacked array:\n",arr2) 

# Output:
# Horizontally stacked array:
#  [[ 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 axes.


# 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("Horizontally stacked array:\n",arr2) 

# Output:
# Horizontally stacked array:
# [[ 2  3  6  8 10]
# [ 4  6  8 10 12]]

Frequently Asked Questions

What does the hstack function in NumPy do?

The hstack function in NumPy is used to horizontally stack arrays. It takes a sequence of input arrays and stacks them along the horizontal axis to create a single array. The arrays must have the same number of rows.

Can hstack be used with arrays of different shapes?

The hstack function in NumPy cannot be used with arrays of different shapes along the horizontal axis. The arrays being horizontally stacked must have the same number of rows. If the arrays have different shapes along the horizontal axis, a ValueError will be raised.

Can hstack be used with both 1-dimensional and 2-dimensional arrays?

The hstack function in NumPy can be used with both 1-dimensional and 2-dimensional arrays. It’s a versatile function that works with arrays of varying dimensions as long as they have the same number of rows.

Can hstack be used with more than two arrays?

The hstack function in NumPy can be used with more than two arrays. You can provide a tuple, list, or any iterable containing the arrays you want to horizontally stack.

Can hstack be used with arrays of different data types?

The hstack function in NumPy can be used with arrays of different data types. When you horizontally stack arrays with different data types, NumPy will automatically upcast the data types to a common data type that can accommodate all the original types.

Is there a similar function for vertical stacking in NumPy?

In NumPy, there is a similar function for vertical stacking called vstack. The vstack function is used to vertically stack arrays. It concatenates the input arrays along the vertical axis, and the arrays must have the same number of columns.

Conclusion

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

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.