当前位置:网站首页>Hstack, vstack and dstack in numpy
Hstack, vstack and dstack in numpy
2022-06-13 02:18:00 【Researcher-Du】
Recently python Code always sees np.hstack, np.vstack as well as np.dstack etc. , Today, I would like to briefly summarize my own understanding . These three stack The changes of can be called horizontal stack(hstack), vertical stack(vstack) And depth stack(dstack), The first two are well understood in two-dimensional arrays , But it is not easy to understand the three-dimensional situation .
1. Two dimensional array
Sample code :
>>> a = np.arange(1,10)
>>> a = a.reshape(3,3)
>>> b = np.arange(10,19)
>>> b = b.reshape(3,3)
>>> np.hstack((a,b))
array([[ 1, 2, 3, 10, 11, 12],
[ 4, 5, 6, 13, 14, 15],
[ 7, 8, 9, 16, 17, 18]])
>>> np.vstack((a,b,c))
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> np.dstack((a,b,c))
array([[[ 1, 10],
[ 2, 11],
[ 3, 12]],
[[ 4, 13],
[ 5, 14],
[ 6, 15]],
[[ 7, 16],
[ 8, 17],
[ 9, 18]]])

2. Three dimensional array
>>> a = np.arange(1,28)
>>> a = a.reshape(3,3,3)
>>> b = np.arange(28,55)
>>> b = b.reshape(3,3,3)
>>> np.hstack((a,b))
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[28, 29, 30],
[31, 32, 33],
[34, 35, 36]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[37, 38, 39],
[40, 41, 42],
[43, 44, 45]],
[[19, 20, 21],
[22, 23, 24],
[25, 26, 27],
[46, 47, 48],
[49, 50, 51],
[52, 53, 54]]])
>>> np.vstack((a,b))
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]],
[[19, 20, 21],
[22, 23, 24],
[25, 26, 27]],
[[28, 29, 30],
[31, 32, 33],
[34, 35, 36]],
[[37, 38, 39],
[40, 41, 42],
[43, 44, 45]],
[[46, 47, 48],
[49, 50, 51],
[52, 53, 54]]])
>>> np.dstack((a,b))
array([[[ 1, 2, 3, 28, 29, 30],
[ 4, 5, 6, 31, 32, 33],
[ 7, 8, 9, 34, 35, 36]],
[[10, 11, 12, 37, 38, 39],
[13, 14, 15, 40, 41, 42],
[16, 17, 18, 43, 44, 45]],
[[19, 20, 21, 46, 47, 48],
[22, 23, 24, 49, 50, 51],
[25, 26, 27, 52, 53, 54]]])

Conclusion : Practical level , The vertical and depth are intuitive in a two-dimensional array , But the stack used to mark 3D arrays is very confusing , therefore vstack Recorded as on page 0 Dimension extension ,hstack Remember to expand in the first dimension ,dstack Recorded as on page 2 Dimension extension is enough . If you treat a three-dimensional array as an image , that ,hstack Indicates the horizontal splicing of two images ,vstack Splice the two images vertically ,dstack Superimpose the channels of the image .
边栏推荐
- [keras] train py
- 传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)
- Functional translation
- Solution of depth learning for 3D anisotropic images
- Jump model between mirrors
- json,xml,txt
- Area of basic exercise circle ※
- Termux SSH first shell start
- [work with notes] NDK compiles the open source library ffmpeg
- [pytorch]fixmatch code explanation (super detailed)
猜你喜欢

Understanding and thinking about multi-core consistency

What are the differences in cache/tlb?

Chapter7-13_ Dialogue State Tracking (as Question Answering)

Huawei equipment is configured with IP and virtual private network hybrid FRR
![How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]](/img/c5/f17333cdb72a1ce09aa54e38dd0a8c.png)
How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]

Mbedtls migration experience

Vivo released originos ocean, and the domestic customized system is getting better and better

C language conditional compilation routine
![[programming idea] communication interface of data transmission and decoupling design of communication protocol](/img/cd/896d1bcad556ffcbf1007bc984afeb.jpg)
[programming idea] communication interface of data transmission and decoupling design of communication protocol

Huawei equipment configures private IP routing FRR
随机推荐
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
Detailed explanation of C language conditional compilation
Understand speech denoising
1、 Set up Django automation platform (realize one click SQL execution)
Application circuit and understanding of BAT54C as power supply protection
STM32F103 IIC OLED program migration complete engineering code
How to do Internet for small enterprises
The fastest empty string comparison method C code
Microsoft Pinyin opens U / V input mode
[keras learning]fit_ Generator analysis and complete examples
Huawei equipment is configured with CE dual attribution
Paipai loan parent company Xinye quarterly report diagram: revenue of RMB 2.4 billion, net profit of RMB 530million, a year-on-year decrease of 10%
Why is Huawei matebook x Pro 2022 leading a "laptop" revolution
[work with notes] NDK compiles the open source library ffmpeg
Uniapp preview function
[pytorch]fixmatch code explanation - data loading
[work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip
Configuring virtual private network FRR for Huawei equipment
记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
[Dest0g3 520迎新赛] 拿到WP还整了很久的Dest0g3_heap