当前位置:网站首页>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 .
边栏推荐
- Stm32+ze-08 formaldehyde sensor tutorial
- C language volatile learning
- Basic exercise of test questions Yanghui triangle (two-dimensional array and shallow copy)
- Bluetooth module: use problem collection
- speech production model
- Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation
- STM32 steering gear controller
- [keras learning]fit_ Generator analysis and complete examples
- synchronized下的 i+=2 和 i++ i++执行结果居然不一样
- Application and examples of C language structure
猜你喜欢

Understand CRF

An image is word 16x16 words: transformers for image recognition at scale
![[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys](/img/fc/f03c7dc4d5ee12aaa301f54e4cd3f4.jpg)
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys

Paper reading - group normalization

【 unity】 Problems Encountered in Packaging webgl Project and their resolution Records

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%

Introduction to arm Cortex-M learning

Mac使用Docker安装Oracle
![[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer](/img/c4/0d97def5fb587b8301bcb907fc6fcf.jpg)
[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer

Huawei equipment is configured with IP and virtual private network hybrid FRR
随机推荐
[Dest0g3 520迎新赛] 拿到WP还整了很久的Dest0g3_heap
Flow chart of interrupt process
How to learn to understand Matplotlib instead of simple code reuse
Yovo3 and yovo3 tiny structure diagram
Mac使用Docker安装Oracle
16 embedded C language interview questions (Classic)
Introduction to armv8/armv9 - learning this article is enough
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
Laptop touch pad operation
ROS learning -5 how function packs with the same name work (workspace coverage)
ROS learning-7 error in custom message or service reference header file
Ctrip reshapes new Ctrip
Understand speech denoising
Understand HMM
Record: how to solve the problem of "the system cannot find the specified path" in the picture message uploaded by transferto() of multipartfile class [valid through personal test]
华为设备配置私网IP路由FRR
Huawei equipment is configured with dual reflectors to optimize the backbone layer of the virtual private network
uniapp 预览功能
Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient
Leetcode 93 recovery IP address