如何将 numpy 数组值复制到更高维度 [英] how to copy numpy array value into higher dimensions
本文介绍了如何将 numpy 数组值复制到更高维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 2d 中有一个 (w,h) np 数组.我想制作一个值大于 1 的 3d 维度,并沿第 3 维度复制其值.我希望广播能做到,但它不能.我就是这样做的
I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would do it but it can't. This is how i'm doing it
arr = np.expand_dims(arr, axis=2)
arr = np.concatenate((arr,arr,arr), axis=2)
有没有更快的方法?
推荐答案
你可以推动所有的暗淡,引入一个单例暗淡/新轴作为最后一个暗淡来创建一个3D
数组,然后使用 np.repeat
,就像这样 -
You can push all dims forward, introducing a singleton dim/new axis as the last dim to create a 3D
array and then repeat three times along that one with np.repeat
, like so -
arr3D = np.repeat(arr[...,None],3,axis=2)
这是另一种使用 np.tile的方法代码>
-
Here's another approach using np.tile
-
arr3D = np.tile(arr[...,None],3)
这篇关于如何将 numpy 数组值复制到更高维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文