The vstack () and hstack () functions in NumPy are two functions for stacking arrays, which are used for vertical stacking and horizontal stacking arrays, respectively. The vstack () function is used to stack the input array vertically, while the hstack () function is used to stack the input array horizontally. By using these two functions, the data structure can be made clearer and easier to understand and analyze. In actual development scenarios, mastering these two functions can help you improve the efficiency of data analysis.
vstack()
Sumhstack()
Are two very useful functions for vertical and horizontal stacking of arrays. These two functions can help us process and analyze data more efficiently.
This article will introduce the usage of these two functions in detail, and show how to apply them in actual development scenarios through examples.
1.vstack()
Function in detail.
\n#What is vstack()
?。
vstack()
Function to stack multiple arrays vertically (rows). It takes as input a tuple or list containing multiple arrays and returns a new array, where each input array becomes a row of the new array.
\n#
Grammar.
numpy.vstack(tup)
\n-tup
: A tuple or list containing multiple arrays. \n#
Sample.
Suppose we have two two-dimensional arrays a
Sumb
:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
We can use vstack()
Stack them vertically:
esult = np.vstack((a, b))
print(result)
The output is:
[[1 2]
[3 4]
[5 6]
[7 8]]
In this example, the array a
Sumb
Are stacked vertically together to form a new array.
2.hstack()
Function in detail.
\n#What is hstack()
?。
hstack()
Function to stack multiple arrays horizontally (columns). It accepts a tuple or list containing multiple arrays as input and returns a new array, where each input array becomes a column of the new array.
\n#
Grammar.
numpy.hstack(tup)
\n-tup
: A tuple or list containing multiple arrays. \n#
Sample.
Suppose we have two two-dimensional arrays c
Sumd
:
= np.array([[1, 2], [3, 4]])
d = np.array([[5, 6], [7, 8]])
We can use hstack()
Stack them horizontally:
esult = np.hstack((c, d))
print(result)
The output is:
[[1 2 5 6]
[3 4 7 8]]
In this example, the array c
Sumd
Stacked horizontally, a new array is formed.
3. Examples in practical applications.
\n#Vertical stacking in data analysis.
In data analysis, we often need to merge multiple data sets into one data set. For example, we have age and income data for two sets of users:
ages = np.array([25, 30, 35])
incomes = np.array([50000, 60000, 70000])
We can use vstack()
Stack them vertically to form a two-dimensional array of age and income:
data = np.vstack((ages, incomes)).T
print(data)
The output is:
[[25 50000]
[30 60000]
[35 70000]]
In this way, we can easily analyze and process the data. \n#
Horizontal stacking in image processing.
In image processing, we may need to stitch multiple images into one large image. For example, we have two pictures img1
Sumimg2
:
img1 = np.random.randint(0, 256, (100, 100))
img2 = np.random.randint(0, 256, (100, 100))
We can use hstack()
Stack them horizontally to form a picture with a width of 200:
ombined_img = np.hstack((img1, img2))
print(combined_img.shape)
The output is:
(100, 200)
In this way, we get a large spliced picture.
4. Summary.
Through the introduction of this article, I believe you have mastered vstack()
Sumhstack()
The basic usage of functions and their application in actual development. These two functions are very useful in data processing and analysis, and can help us organize and process data more efficiently.
Hope these contents are helpful to you and can play a bigger role in the actual project.