numpy column_stack循环中

时间:2019-01-05 13:00:55

标签: python loops numpy

我不知道标题是否合适,但让我告诉你我想做什么,

In [56]: import numpy as np

In [57]: a= np.random.rand(2,2,2); a
Out[57]: 
array([[[0.4300565 , 0.82251319],
        [0.56113378, 0.83284255]],

       [[0.00822414, 0.28256243],
        [0.16648411, 0.33381438]]])

In [58]: b=np.random.rand(2); b
Out[58]: array([0.8035224 , 0.09884653])

In [59]: np.stack(( np.column_stack((b,a[:,i,:])) for i in range(a.shape[1])))
Out[59]: 
array([[[0.8035224 , 0.4300565 , 0.82251319],
        [0.09884653, 0.00822414, 0.28256243]],

       [[0.8035224 , 0.56113378, 0.83284255],
        [0.09884653, 0.16648411, 0.33381438]]])

因此,我想将一个数组堆叠为内轴的列。是否可以在numpy中更高效,更简洁地执行循环结构?我尝试使用numpy insert,但无法做到。

编辑:

另一个例子

In [110]: a= np.random.rand(5,3,3); a
Out[110]: 
array([[[0.27506756, 0.82334411, 0.7004287 ],
        [0.6834928 , 0.28457133, 0.6275462 ],
        [0.49744358, 0.25131814, 0.56422852]],

       [[0.82591597, 0.92367306, 0.04652992],
        [0.98545051, 0.92813944, 0.14360307],
        [0.85454081, 0.8254149 , 0.5637401 ]],

       [[0.59545519, 0.41563571, 0.41937218],
        [0.90980491, 0.30169504, 0.96630809],
        [0.06713389, 0.64357544, 0.12901734]],

       [[0.47566444, 0.33476802, 0.26635363],
        [0.4678913 , 0.53028241, 0.03112231],
        [0.68445959, 0.07113376, 0.86651669]],

       [[0.66951982, 0.01827502, 0.43831829],
        [0.02798567, 0.36880876, 0.55029074],
        [0.40127051, 0.6311474 , 0.51015882]]])

In [111]:  b= np.random.rand(5,2); b
Out[111]: 
array([[0.01659589, 0.15320541],
       [0.79025065, 0.28041334],
       [0.56024173, 0.49317082],
       [0.28229119, 0.46010724],
       [0.72239851, 0.62075004]])

In [112]: np.stack(( np.column_stack((b,a[:,i,:])) for i in range(a.shape[1])))
Out[112]: 
array([[[0.01659589, 0.15320541, 0.27506756, 0.82334411, 0.7004287 ],
        [0.79025065, 0.28041334, 0.82591597, 0.92367306, 0.04652992],
        [0.56024173, 0.49317082, 0.59545519, 0.41563571, 0.41937218],
        [0.28229119, 0.46010724, 0.47566444, 0.33476802, 0.26635363],
        [0.72239851, 0.62075004, 0.66951982, 0.01827502, 0.43831829]],

       [[0.01659589, 0.15320541, 0.6834928 , 0.28457133, 0.6275462 ],
        [0.79025065, 0.28041334, 0.98545051, 0.92813944, 0.14360307],
        [0.56024173, 0.49317082, 0.90980491, 0.30169504, 0.96630809],
        [0.28229119, 0.46010724, 0.4678913 , 0.53028241, 0.03112231],
        [0.72239851, 0.62075004, 0.02798567, 0.36880876, 0.55029074]],

       [[0.01659589, 0.15320541, 0.49744358, 0.25131814, 0.56422852],
        [0.79025065, 0.28041334, 0.85454081, 0.8254149 , 0.5637401 ],
        [0.56024173, 0.49317082, 0.06713389, 0.64357544, 0.12901734],
        [0.28229119, 0.46010724, 0.68445959, 0.07113376, 0.86651669],
        [0.72239851, 0.62075004, 0.40127051, 0.6311474 , 0.51015882]]])

2 个答案:

答案 0 :(得分:1)

连接的一种变化是索引分配:

第一个示例:

In [245]: a=np.arange(8).reshape(2,2,2); b=np.array([100,200])
In [246]: c = np.zeros((2,2,3), a.dtype)
In [247]: c[:,:,0]=b
In [248]: c[:,:,1:]=a.transpose(1,0,2)
In [249]: c
Out[249]: 
array([[[100,   0,   1],
        [200,   4,   5]],

       [[100,   2,   3],
        [200,   6,   7]]])

第二个:

In [250]: a1 = np.arange(5*3*3).reshape(5,3,3)
In [251]: b1 = np.arange(10).reshape(5,2)
In [252]: c1 = np.zeros((3,5,5),a.dtype)
In [253]: c1[:,:,:2]=b1
In [254]: c1[:,:,2:]=a1.transpose(1,0,2)
In [255]: c1
Out[255]: 
array([[[ 0,  1,  0,  1,  2],
        [ 2,  3,  9, 10, 11],
        [ 4,  5, 18, 19, 20],
        [ 6,  7, 27, 28, 29],
        [ 8,  9, 36, 37, 38]],

       [[ 0,  1,  3,  4,  5],
        [ 2,  3, 12, 13, 14],
        [ 4,  5, 21, 22, 23],
        [ 6,  7, 30, 31, 32],
        [ 8,  9, 39, 40, 41]],

       [[ 0,  1,  6,  7,  8],
        [ 2,  3, 15, 16, 17],
        [ 4,  5, 24, 25, 26],
        [ 6,  7, 33, 34, 35],
        [ 8,  9, 42, 43, 44]]])

留给读者练习从ca衍生b的形状。 :)

np.stack(或np.array)在第二个轴上的迭代实际上是部分转置(或前两个轴的互换):

In [261]: np.stack([a[:,i,:] for i in range(a.shape[1])])
Out[261]: 
array([[[0, 1],
        [4, 5]],

       [[2, 3],
        [6, 7]]])
In [262]: a.transpose(1,0,2)
Out[262]: 
array([[[0, 1],
        [4, 5]],

       [[2, 3],
        [6, 7]]])

我们还可以在第一个轴上进行迭代,然后在第二个轴上使用:

In [263]: np.stack(a, axis=1)
Out[263]: 
array([[[0, 1],
        [4, 5]],

       [[2, 3],
        [6, 7]]])

使用concatenateAnkit's answer的改进是:

np.concatenate([np.repeat(b[None,:,None], 2, axis=0), a.transpose(1,0,2)], axis=2)
np.concatenate([np.repeat(b1[None,:,:], 3, axis=0), a1.transpose(1,0,2)], axis=2)

答案 1 :(得分:0)

下面的代码对我有用。

>>> a= np.random.rand(2,2,2); a
array([[[0.52706506, 0.48344319],
        [0.79027196, 0.90581149]],

       [[0.25930158, 0.59498346],
        [0.02164495, 0.63081622]]])
>>> b=np.random.rand(2); b
array([0.96890722, 0.93670425])
>>> a1 = a.transpose(1, 0, 2); a1
array([[[0.52706506, 0.48344319],
        [0.25930158, 0.59498346]],

       [[0.79027196, 0.90581149],
        [0.02164495, 0.63081622]]])
>>> c = np.tile(b, (2, 1)); c
array([[0.43134454, 0.4042494 ],
       [0.43134454, 0.4042494 ]])
>>> c = np.expand_dims(c,2); c
array([[[0.43134454],
        [0.4042494 ]],

       [[0.43134454],
        [0.4042494 ]]])
>>> np.concatenate((c, a1), axis=2)
array([[[0.43134454, 0.52706506, 0.48344319],
        [0.4042494 , 0.25930158, 0.59498346]],

       [[0.43134454, 0.79027196, 0.90581149],
        [0.4042494 , 0.02164495, 0.63081622]]])

在这里,我首先在新尺寸中使用与a的第二尺寸相同的数字使用tile重复b。 然后我使用了隐式方法来连接b和一个数组。

第二个例子

>>> a= np.random.rand(5,3,3)
>>> a1 = a.transpose(1, 0, 2)
>>> b=np.random.rand(5, 2)
>>> c = np.tile(b, (3, 1, 1))
>>> np.concatenate((c, a1), axis=2)