重复/复制给定的numpy数组十次 [英] Repeat/duplicate a given numpy array ten times

查看:473
本文介绍了重复/复制给定的numpy数组十次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的形状为(320、320、3)的Numpy array.我想重复/复制此数据10次,并想要获得新的形状数组( 10 ,320、320、3).

I have a numpy array of shape, (320, 320, 3). I want to repeat/duplicate this data 10 times, and want to get new array of shape (10, 320, 320, 3).

该怎么做?

array = np.ones((320, 320, 3))
print (array.shape)
(320, 320, 3)

我尝试过:

res = np.tile(array, 10)
print (res.shape)

(320, 320, 30).

但我要shape个,

(10, 320, 320, 3)

推荐答案

我们可以使用

We can use np.broadcast_to -

np.broadcast_to(a,(10,)+a.shape).copy() # a is input array

如果我们可以使用视图,则可以跳过.copy()以获得几乎免费的运行时和零内存开销.

If we are okay with a view instead, skip .copy() for a virtually free runtime and zero memory overhead.

我们还可以使用 np.repeat -

We can also use np.repeat -

np.repeat(a[None],10,axis=0)

这篇关于重复/复制给定的numpy数组十次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆