Hstack python – Python NumPy hstack() Function

NumPy hstack() Function:

Hstack python: The hstack() function is used to stack arrays horizontally in sequence (column-wise).
Except for 1-D arrays, where it concatenates along the first axis, this is equivalent to concatenation along the second axis. Rebuilds arrays that have been divided by hsplit.
This function is best suited for arrays with up to three dimensions. For example, consider pixel data with height (first axis), width (second axis), and r/g/b channels (third axis). More general stacking and concatenation operations are provided by the functions concatenate, stack, and block.

Syntax:

numpy.hstack(tuple)

Parameters

tuple: This is required. It is the sequence of ndarrays that we want to stack horizontally. Except for 1-D arrays, which can be any length, the arrays must have the same shape along all but the second axis.

Return Value: 

Numpy hstack: The array formed by stacking the specified arrays is returned.

NumPy hstack() Function in Python

Example1

Here we used arrays of equal shape(dimensions).

Approach:

  • Import numpy module using the import keyword.
  • Give the first array as static input using the array() function and store it in a variable.
  • Give the second array as static input using the array() function and store it in another variable.
  • Print the shape(dimensions) of the given first array using the shape function.
  • Print the given first array.
  • Print the shape(dimensions) of the given second array using the shape function.
  • Print the given second array.
  • Pass the above given 2 arrays as arguments to the hstack() function to get a horizontally stacked array.
  • Store it in another variable.
  • Print the shape of the above stack array using the shape function.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy
# Give the first array as static input using the array() function
# and store it in a variable
gvn_arry1= numpy.array([[1,2],[3, 4]])
# Give the second array as static input using the array() function and
# store it in another variable
gvn_arry2 = numpy.array([[5,6],[8,9]])
# Print the shape(dimensions) of the given first array using the shape function
print("Given First array shape is:", gvn_arry1.shape)
#  Print the given first array
print("The given first array is:\n", gvn_arry1)
# Print the shape of the given second array using the shape function
print("Given second array shape is:", gvn_arry2.shape)
# Print the given second array
print("The given second array is:\n", gvn_arry2)
print()
# Pass the above given 2 arrays as arguments to the hstack() function
# to get a horizontally stacked array.
# Store it in another variable
stack_arry = numpy.hstack((gvn_arry1, gvn_arry2))
print("The Horizontally Stacked array for the above given 2 arrays is:")
print(stack_arry)
# Print the shape of the above stack array using the shape function
print("The result stacked array shape is:", stack_arry.shape)

Output:

Given First array shape is: (2, 2)
The given first array is:
[[1 2]
[3 4]]
Given second array shape is: (2, 2)
The given second array is:
[[5 6]
[8 9]]

The Horizontally Stacked array for the above given 2 arrays is:
[[1 2 5 6]
[3 4 8 9]]
The result stacked array shape is: (2, 4)

Example2

Python hstack: Here the two arrays have identical shape, except for a different second axis.

Below is the implementation:

# Import numpy module using the import keyword
import numpy
# Give the first array as static input using the array() function
# and store it in a variable
gvn_arry1= numpy.array([[1,2],[3, 4]])
# Give the second array as static input using the array() function and
# store it in another variable
gvn_arry2 = numpy.array([[5,6,7],[8,9,10]])
# Print the shape(dimensions) of the given first array using the shape function
print("Given First array shape is:", gvn_arry1.shape)
#  Print the given first array
print("The given first array is:\n", gvn_arry1)
# Print the shape of the given second array using the shape function
print("Given second array shape is:", gvn_arry2.shape)
# Print the given second array
print("The given second array is:\n", gvn_arry2)
print()
# Pass the above given 2 arrays as arguments to the hstack() function
# to get a horizontally stacked array.
# Store it in another variable
stack_arry = numpy.hstack((gvn_arry1, gvn_arry2))
print("The Horizontally Stacked array for the above given 2 arrays is:")
print(stack_arry)
# Print the shape of the above stack array using the shape function
print("The result stacked array shape is:", stack_arry.shape)

Output:

Given First array shape is: (2, 2)
The given first array is:
[[1 2]
 [3 4]]
Given second array shape is: (2, 3)
The given second array is:
[[ 5 6 7]
 [ 8 9 10]]

The Horizontally Stacked array for the above given 2 arrays is:
[[ 1 2 5 6 7]
 [ 3 4 8 9 10]]
The result stacked array shape is: (2, 5)