# numpy.hstack() – The NumPy hstack Python Function

## Introduction to NumPy hstack Python Function

Guide to NumPy hstack Python Function – The numpy.hstack() function is used to build a single array by stacking the sequence of input arrays horizontally (i.e. column wise).

Syntax:

numpy.hstack(tup)

Parameters:

• tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the second axis.

Return: [stacked ndarray] The stacked array of the input arrays.

## Code Examples of NumPy hstack Python Function

Example 01:

# welcome to softhunt.net
# Python program explaining
# hstack() function

import numpy as np

# input array
in_arr1 = np.array([0, 1, 3] )
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array([5, 7, 9] )
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays horizontally
out_arr = np.hstack((in_arr1, in_arr2))
print ("Output horizontally stacked array:\n ", out_arr)

Output:

1st Input array :
[0 1 3]
2nd Input array :
[5 7 9]
Output horizontally stacked array:
[0 1 3 5 7 9]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Example 02:

# welcome to softhunt.net
# Python program explaining
# hstack() function

import numpy as np

# input array
in_arr1 = np.array([[0, 1, 3], [-0, -1, -3]] )
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array([[5, 7, 9], [-5, -7, -9]] )
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays horizontally
out_arr = np.hstack((in_arr1, in_arr2))
print ("Output stacked array :\n ", out_arr)

Output:

1st Input array :
[[ 0  1  3]
[ 0 -1 -3]]
2nd Input array :
[[ 5  7  9]
[-5 -7 -9]]
Output stacked array :
[[ 0  1  3  5  7  9]
[ 0 -1 -3 -5 -7 -9]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

## FAQs

### What is the Difference Between numpy.hstack and numpy.concatenate?

numpy.hstack is essentially a particular instance of numpy.concatenate. Unlike numpy.hstack, which can only join arrays horizontally, and numpy.vstack, which can only combine them vertically, numpy.concatenate may combine them in any way.

Another way to put it is that numpy.concatenate is similar to numpy.vstack or numpy.hstack but more general and flexible.

### What is the Difference Between numpy.hstack and numpy.vstack?

numpy.hstack and numpy.vstack are both NumPy stacks that mix NumPy arrays. The main distinction is that numpy.hstack stacks NumPy arrays horizontally, whereas numpy.vstack stacks them vertically. Aside from that, syntax and behavior are nearly identical.

### The NumPy Stack Python Function

To join a sequence of same-dimension arrays along a new axis, use the numpy.stack() function. The axis parameter defines the new axis index in the result’s dimensions. For example, if axis=0, the first dimension will be 0, and if axis=-1, the last dimension would be 1.

## Conclusion

That’s all for NumPy hstack article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles: