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.
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.
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
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.
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.
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.
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.
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.
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!!
Related Articles
- How to Use NumPy Random choice() in Python
- How to Use NumPy random.normal() In Python
- Create an array using arange() function
- Python NumPy repeat() Function
- How to get average of an array
- Python NumPy Reverse Array
- How to use NumPy vstack() in Python
- Python NumPy Absolute Value
- Python NumPy repeat() Function
- How to Use NumPy stack() in Python
- How to Compute Standard Deviation in NumPy
- Python NumPy Split Array – Using split() Function