简体   繁体   English

numpy数组结构与广播

[英]numpy array construction with broadcasting

I would like stack together arrays that have different, but broadcast compatible arrays. 我想将具有不同但广播兼容阵列的阵列堆叠在一起。 Given 7x5, 7x1, 1x5 and 1x1 arrays I want to do something like 给定7x5,7x1,1x5和1x1阵列,我想做类似的事情

 a475 = mkarr([a75, a71, a15, a11])

where a455 will be a 4x7x5 array. 其中a455将是一个4x7x5阵列。 Ideally I would also like to be able to do 理想情况下,我也希望能够做到

 a2275 = mkarr([[a75, a71], [a15, a11]])

to get a 2x2x7x5 array. 获得2x2x7x5阵列。

What is the most concise way to express these operation in numpy? 在numpy中表达这些操作最简洁的方法是什么?

You can use np.broadcast_arrays : 你可以使用np.broadcast_arrays

a475 = np.stack(np.broadcast_arrays(a75, a71, a15, a11))

Note that this creates views of the original arrays, so it should not entail any extra memory usage. 请注意,这会创建原始数组的视图,因此不应占用任何额外的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM