具有交错的Numpy串联数组 [英] Numpy concatenate arrays with interleaving
本文介绍了具有交错的Numpy串联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有4个数组,我想通过交错将它们连接成一个数组。我该怎么做?
I have 4 arrays and I want to concatenate them into one single array with interleaving. How do I do this?
>>> import numpy as np
>>> a = np.tile(0,(5,2))
>>> b = np.tile(1,(5,2))
>>> c = np.tile(2,(5,2))
>>> d = np.tile(3,(5,2))
>>> e = np.concatenate((a,b,c,d),axis=1)
>>> e
array([[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3]])
这只是串联。
不过,我的期望输出是:
However, my desired_output is:
>>> desired_output
array([[0, 1, 2, 3, 0, 1, 2, 3],
[0, 1, 2, 3, 0, 1, 2, 3],
[0, 1, 2, 3, 0, 1, 2, 3],
[0, 1, 2, 3, 0, 1, 2, 3],
[0, 1, 2, 3, 0, 1, 2, 3]])
我是说我知道我可以使用以下命令从e中选择交错的列:
I mean I know I can select the interleaved columns from e using:
>>> f = e[:, ::2]
>>> array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
但是我如何制作一个大数组?
But how do I make one big array?
推荐答案
使用 np.dstack
或 np.stack
沿最后一个轴堆叠,从而得到一个 3D
数组,然后重塑为 2D
-
Use np.dstack
or np.stack
to stack along the last axis that gives us a 3D
array and then reshape back to 2D
-
np.dstack([a,b,c,d]).reshape(a.shape[0],-1)
np.stack([a,b,c,d],axis=2).reshape(a.shape[0],-1)
这篇关于具有交错的Numpy串联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文