Introduction to NumPy hstack
Whenever there we have more than one arrays and we wish to display the values present in those arrays together sequentially or stack them horizontally one after the other in a single array, we make use of a function called hstack function in NumPy.This hstack function in NumPy returns a horizontally stacked array from more than one arrays which are used as the input to the hstack function and this hstack function takes a tuple as an argument which represents the input arrays those are to be stacked resulting in a single array and the input arrays passed to this hstack function in NumPy must be of the similar shape.
Syntax:
numpy.hstack(tuple)
where tuple represents the input arrays those are to stacked resulting in a single array.
Working of NumPy hstack
Working of NumPy hstack is as follows:
- Whenever there we have more than one arrays and we wish to display the values present in those arrays together sequentially or stack them horizontally one after the other in a single array, we make use of a function called hstack in NumPy.
- It returns a horizontally stacked array from more than one arrays which are used as the input to the hstack function.
- It takes a tuple as an arguments which represents the input arrays that are to be stacked resulting in a single array.
- The input arrays passed to this hstack function in NumPy must be of a similar shape.
Examples of NumPy hstack
Following are the examples as shown below
Example #1
Python program to demonstrate NumPyhstack function to horizontally stack the given two input arrays into a single array and display the resulting array as the output on the screen:
Code:
#importing the package numpy
import numpy as num
#Creating an array by making use of array function in NumPy and storing it in a variable called firstarray
firstarray = num.array([[1,2,3],[4,5,6]])
#Displaying the elements of firstarray followed by one line space by making use of \n
print 'The elements of the first array are:'
print firstarray
print '\n'
#Creating an array by making use of array function in NumPy and storing it in a variable called secondarray
secondarray = num.array([[7,8,9],[10,11,12]])
#displaying the elements of second array followed by one line space by making use of \n
print 'The elements of the second array are:'
print secondarray
print '\n'
print 'The elements of the array after stacking the given two arrays horizontally by making use of hstack function are:'
#creating an array by horizontally stacking the elements of firstarray and secondarray by making use of hstack function and storing it in a variable called resultingarray
resultingarray = num.hstack((firstarray,secondarray))
#displaying the elements of the resultingarray followed by one line space by making use of \n
print resultingarray
print '\n'
Output:
In the above program, a package called NumPy is imported to enable us to make use of hstack function. Then an array is created by making use of the array function in NumPy and storing it in a variable called firstarray. Then the elements of the firstarray are displayed on the screen. Then another array is created by making use of array function in NumPy and storing it in a variable called secondarray. Then the elements of the secondarray are displayed on the screen. Then an array is created by horizontally stacking the elements of firstarray and secondarray by making use of hstack function and storing it in a variable called resultingarray. Then the elements of the resultingarray is displayed on the screen.
Example #2
Python program to demonstrate NumPyhstack function to horizontally stack the given two input arrays into a single array and display the resulting array as the output on the screen:
Code:
#importing the package numpy
import numpy as num
#Creating an array by making use of array function in NumPy and storing it in a variable called firstarray
firstarray = num.array([[1,2,3,13],[4,5,6,14]])
#Displaying the elements of firstarray followed by one line space by making use of \n
print 'The elements of the first array are:'
print firstarray
print '\n'
#Creating an array by making use of array function in NumPy and storing it in a variable called secondarray
secondarray = num.array([[7,8,9,15],[10,11,12,16]])
#displaying the elements of second array followed by one line space by making use of \n
print 'The elements of the second array are:'
print secondarray
print '\n'
print 'The elements of the array after stacking the given two arrays horizontally by making use of hstack function are:'
#creating an array by horizontally stacking the elements of firstarray and secondarray by making use of hstack function and storing it in a variable called resultingarray
resultingarray = num.hstack((firstarray,secondarray))
#displaying the elements of the resultingarray followed by one line space by making use of \n
print resultingarray
print '\n'
Output:
In the above program, a package called NumPy is imported to enable us to make use of hstack function. Then an array is created by making use of array function in NumPy and storing it in a variable called firstarray. Then the elements of the firstarray are displayed on the screen. Then another array is created by making use of array function in NumPy and storing it in a variable called secondarray. Then the elements of the secondarray are displayed on the screen. Then an array is created by horizontally stacking the elements of firstarray and secondarray by making use of hstack function and storing it in a variable called resultingarray. Then the elements of the resultingarray is displayed on the screen.
Conclusion
In this tutorial, we understand the concept of NumPyhstack function in Python through definition, the syntax of NumPy hstack function, and the working of NumPyhstack function in Python through programming examples and their outputs.
Recommend ed Articles
This is a guide to NumPy hstack. Here we also discuss the introduction and working of numpy hstack along with different examples and its code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses