简体   繁体   English

如何向 numpy 数组添加维度并从另一个 numpy 数组复制维度

[英]How do i add a dimension to a numpy array and copy the dimension from another numpy array

I have a numpy array with the shape (128, 8) I want to add an extra dimension so it has the shape (128, 168, 8) And add the content of a 168 dimension from another array that has the shape (128, 168, 8).我有一个形状为 (128, 8) 的 numpy 数组我想添加一个额外的维度,使其具有形状 (128, 168, 8) 并从另一个形状为 (128, 168, 8)。 I can always permute the positions of the dimensions if I can somehow add it.如果我能以某种方式添加它,我总是可以置换尺寸的位置。 Is this possible somehow?这有可能吗? I have seen the append and concatenation methods but to no luck.我见过 append 和连接方法,但没有运气。

np.expand_dims(smaller_array, axis=1) + bigger_array

Is the correct solution, thanks!是正确的解决方案,谢谢!

you can also do:你也可以这样做:

small[:,None,:]+big

Adding None to indexing creates a new dimension of size 1, and adding to another bigger array will broadcast the small's size=1 dimension to bigger arrays corresponding dimension size (here will be 168)None添加到索引会创建一个大小为 1 的新维度,并且添加到另一个更大的数组会将小的 size=1 维度广播到更大的 arrays 相应的维度大小(这里将是 168)

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

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