Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use NumPy to realize horizontal combination array and vertical combination array

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use NumPy to achieve horizontal combination array and vertical combination array related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this article on how to use NumPy to achieve horizontal combination array and vertical combination array will have a harvest, let's take a look together.

1 horizontal array combination

You can combine two or more arrays horizontally to form an array through the hstack function, so what is the horizontal combination of arrays? Let's take a look at an example.

There are now two arrays An and B of 3 to 2.

Array A

0 1 2 3 4 5

Array B

6 7 8 4 1 5

Now the code to combine the two arrays horizontally using the hstack function is as follows.

Hstack (A & M B)

The return value of the hstack function is the result of the combination.

0 1 2 6 7 8 3 4 5 4 1 5

We can see that array An and the number B are connected horizontally to form a new array. This is the horizontal combination of arrays. The effect of horizontal combination of multiple arrays is similar. However, the horizontal combination of arrays must meet the condition that the number of rows of all arrays participating in the horizontal combination must be the same, otherwise horizontal combination will throw an exception.

The following example creates three two-dimensional arrays (with the same number of rows) through the reshape method and multiplication, and then combines two or three of them horizontally using the hstack function.

From numpy import * a = arange (9). Reshape (3) b = a * 3 print (a) print ('-') print (b) print ('-') c = a * 5 # horizontal combination an and b print ((a) B)) print ('-') # horizontal combination a, b and c print (hstack (arecinbpencec)

The running result of the program is shown in figure 1.

Figure 1 horizontal combination array

2 vertical array combination

Two or more arrays can be vertically combined to form an array through the vstack function, so what is the vertical combination of arrays? Let's take a look at an example.

There are now two arrays An and B of 3 to 2.

Array A

0 1 2 3 4 5

Array B

6 7 8 4 1 5

Now the code to combine the two arrays vertically using the vstack function is as follows.

Vstack (A & M B)

The return value of the vstack function is the result of the combination.

0 1 2 3 4 5 6 7 8 4 1 5

The following example creates three two-dimensional arrays (with the same number of rows) through the reshape method and multiplication, and then combines two or three of them horizontally using the hstack function.

From numpy import * a = arange (12) .reshape (3) 4) b = arange (16) .reshape (4) c = arange (20) .reshape (5) print (a) print ('-') print (b) print ('-') print (c) print ('-') # Vertical combination print (vstack (arect bpenc)

The running result of the program is shown in figure 2.

Figure 2 Vertical combination array

This is the end of the article on "how to use NumPy to implement horizontal and vertical combinational arrays". Thank you for reading! I believe that everyone has a certain understanding of "how to use NumPy to achieve horizontal combination array and vertical combination array" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report