我有一个2维的numpy数组
a = array([[1,2],[3,4]])
我的要求是:
array([[1,1,2,2],
[1,1,2,2],
[3,3,4,4],
[3,3,4,4]])
我可以通过两次调用
numpy.repeat
来实现这个功能。repeat(repeat(a,2,axis=0),2,axis=1)
但是,有没有一些参数的组合可以通过单个调用来实现这一点?
我有一个2维的numpy数组
a = array([[1,2],[3,4]])
array([[1,1,2,2],
[1,1,2,2],
[3,3,4,4],
[3,3,4,4]])
numpy.repeat
来实现这个功能。repeat(repeat(a,2,axis=0),2,axis=1)
numpy.lib.stride_tricks.as_strided()
来创建这样的数组:s = min(a.strides)
as_strided(a, shape=(2,2,2,2), strides=(2*s,0,s,0)).reshape(4,4)
#array([[1, 1, 2, 2],
# [1, 1, 2, 2],
# [3, 3, 4, 4],
# [3, 3, 4, 4]])
< p > reshape()
函数会复制数据,在最后创建一个连续的数组。
注意:虽然可以使用单个调用完成此操作,但在我的计算机上,您的原始解决方案要快4倍。