hstack and vstack in numpy | vstack vs hstack
In this numpy tutorial, we will discuss about:
-
What is stacking?
-
hstack arrays in numpy,
-
vstack in numpy,
-
vstack and hstack in numpy,
Before we move ahead to learn about method hstack in numpy, that will help to stack the arrays horizontally as well as vertically in python, lets create one numpy array.
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module in which we have to import from the python.
Syntax to import:
import numpy
We can also use alias for the module
For example,
import numpy as np
We can directly use np to call the numpy module.
Create Numpy Array
An array is an one dimensional data structure used to store single data type data.
I.E It will only store all integer data or all string type data.or all float type data.
We can create an numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
What is stacking?
Stacking means placing elements from two or more arrays.
So there are two possibilities:
-
one is horizontal stacking and
-
other is vertical stacking.
Lets see how to use hstack arrays in numpy.
Scenario 1 : Horizontal Stacking using hstack in numpy
We can make a horizontal stacking using hstack() method.
Syntax:
numpy.hstack((array_data1, array_data2))
where,
-
array_data1 is the first numpy input array
-
array_data2 is the second numpy input array
Example: hstack command in numpy
In this hstack arrays in numpy example, we are stacking two numpy arrays horizontally.
#importing the numpy module
import numpy
#create an array with 8 elements - integer type
array_data1=numpy.array([34,56,43,22,45,6,54,2])
#actual array1
print(array_data1)
#create an array with 8 elements - integer type
array_data2=numpy.array([1,2,3,4,5,6,7,8])
#actual array2
print(array_data2)
print()
print(numpy.hstack((array_data1, array_data2)))
Output: hstack command in numpy result
Displaying the actual numpy arrays and horizontal stacked arrays.
[34 56 43 22 45 6 54 2]
[1 2 3 4 5 6 7 8]
[34 56 43 22 45 6 54 2 1 2 3 4 5 6 7 8]
Now lets see how to use vstack in numpy.
Scenario 2 : Vertical Stacking using vstack in numpy
We can make a vertical stacking using vstack() method or vstack in numpy.
Syntax:
numpy.vstack((array_data1, array_data2))
where,
-
array_data1 is the first numpy input array
-
array_data2 is the second numpy input array
Example: vstack in numpy array
In this vstack in numpy array example, we are stacking two numpy arrays vertically.
#importing the numpy module
import numpy
#create an array with 8 elements - integer type
array_data1=numpy.array([34,56,43,22,45,6,54,2])
#actual array1
print(array_data1)
#create an array with 8 elements - integer type
array_data2=numpy.array([1,2,3,4,5,6,7,8])
#actual array2
print(array_data2)
print()
#vertical stacking
print(numpy.vstack((array_data1, array_data2)))
Output: vstack in numpy array result
Displaying the actual numpy arrays and vertical stacked arrays.
[34 56 43 22 45 6 54 2]
[1 2 3 4 5 6 7 8]
[[34 56 43 22 45 6 54 2]
[ 1 2 3 4 5 6 7 8]]
Since we have seen both method so we can easily compare vstack and hstack in numpy or vstack vs hstack.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 22,2022