4

您好,我有 3 个 numpy 数组,如下所示。

>>> print A
[[ 1.  0.  0.]
 [ 3.  0.  0.]
 [ 5.  2.  0.]
 [ 2.  0.  0.]
 [ 1.  2.  1.]]
>>> print B
[[  5.   9.   9.]
 [ 37.   8.   9.]
 [ 49.   8.   3.]
 [  3.   3.   1.]
 [  4.   4.   5.]]
>>> 
>>> print C
[[ 0.  0.  0.]
 [ 0.  6.  0.]
 [ 1.  4.  6.]
 [ 6.  2.  0.]
 [ 0.  5.  4.]]

我想将它们组合为

[[[  1.   0.   0.]
  [  5.   9.   9.]
  [  0.   0.   0.]]

 [[  3.   0.   0.]
  [ 37.   8.   9.]
  [  0.   6.   0.]]

 [[  5.   2.   0.]
  [ 49.   8.   3.]
  [  1.   4.   6.]]

 [[  2.   0.   0.]
  [  3.   3.   1.]
  [  6.   2.   0.]]

 [[  1.   2.   1.]
  [  4.   4.   5.]
  [  0.   5.   4.]]]

那就是我想从每个数组中取一行。谁能告诉我一个简单的方法来做到这一点?我已经试过了hstackvstack。但他们没有给出预期的结果。

谢谢 !

4

6 回答 6

9

使用np.stack使这变得微不足道:

>>> np.stack([A, B, C], axis=1)  # stack along a new axis in axis 1 of the result
array([[[ 1,  0,  0],
        [ 5,  9,  9],
        [ 0,  0,  0]],

       [[ 3,  0,  0],
        [37,  8,  9],
        [ 0,  6,  0]],

       [[ 5,  2,  0],
        [49,  8,  3],
        [ 1,  4,  6]],

       [[ 2,  0,  0],
        [ 3,  3,  1],
        [ 6,  2,  0]],

       [[ 1,  2,  1],
        [ 4,  4,  5],
        [ 0,  5,  4]]])
于 2016-09-22T22:19:55.107 回答
5

使用 numpy 的解决方案dstack

>>> import numpy as np
>>> np.dstack((a,b,c)).swapaxes(1,2)
array([[[ 1,  0,  0],
        [ 5,  9,  9],
        [ 0,  0,  0]],

       [[ 3,  0,  0],
        [37,  8,  9],
        [ 0,  6,  0]],

       [[ 5,  2,  0],
        [49,  8,  3],
        [ 1,  4,  6]],

       [[ 2,  0,  0],
        [ 3,  3,  1],
        [ 6,  2,  0]],

       [[ 1,  2,  1],
        [ 4,  4,  5],
        [ 0,  5,  4]]])
于 2013-09-03T15:03:20.930 回答
4
>>> np.hstack([a,b,c]).reshape((5,3,3))
array([[[  1.,   0.,   0.],
        [  5.,   9.,   9.],
        [  0.,   0.,   0.]],

       [[  3.,   0.,   0.],
        [ 37.,   8.,   9.],
        [  0.,   6.,   0.]],

       [[  5.,   2.,   0.],
        [ 49.,   8.,   3.],
        [  1.,   4.,   6.]],

       [[  2.,   0.,   0.],
        [  3.,   3.,   1.],
        [  6.,   2.,   0.]],

       [[  1.,   2.,   1.],
        [  4.,   4.,   5.],
        [  0.,   5.,   4.]]])
于 2013-09-03T15:04:53.307 回答
2

无需使用vstack, hstack。只需使用以下命令交换轴np.swapaxes

>>> d=array([a, b, c])
>>> d
array([[[ 1,  0,  0],
        [ 3,  0,  0],
        [ 5,  2,  0],
        [ 2,  0,  0],
        [ 1,  2,  1]],

       [[ 5,  9,  9],
        [37,  8,  9],
        [49,  8,  3],
        [ 3,  3,  1],
        [ 4,  4,  5]],

       [[ 0,  0,  0],
        [ 0,  6,  0],
        [ 1,  4,  6],
        [ 6,  2,  0],
        [ 0,  5,  4]]])
>>> swapaxes(d, 0, 1)
array([[[ 1,  0,  0],
        [ 5,  9,  9],
        [ 0,  0,  0]],

       [[ 3,  0,  0],
        [37,  8,  9],
        [ 0,  6,  0]],

       [[ 5,  2,  0],
        [49,  8,  3],
        [ 1,  4,  6]],

       [[ 2,  0,  0],
        [ 3,  3,  1],
        [ 6,  2,  0]],

       [[ 1,  2,  1],
        [ 4,  4,  5],
        [ 0,  5,  4]]])
于 2013-09-03T17:49:36.530 回答
2
>>> import numpy as np
>>> A = np.array([[1,0,0],[3,0,0],[5,2,0],[2,0,0],[1,2,1]])
>>> B = np.array([[5,9,9],[37,8,9],[49,8,3],[3,3,1],[4,4,5]])
>>> C = np.array([[0,0,0],[0,6,0],[1,4,6],[6,2,0],[0,5,4]])
>>> np.array([A,B,C]).swapaxes(1,0)

array([[[ 1,  0,  0],
    [ 5,  9,  9],
    [ 0,  0,  0]],

   [[ 3,  0,  0],
    [37,  8,  9],
    [ 0,  6,  0]],

   [[ 5,  2,  0],
    [49,  8,  3],
    [ 1,  4,  6]],

   [[ 2,  0,  0],
    [ 3,  3,  1],
    [ 6,  2,  0]],

   [[ 1,  2,  1],
    [ 4,  4,  5],
    [ 0,  5,  4]]])

我使用 Ipython 比较了答案%%timeit

np.array([A,B,C]).swapaxes(1,0)
100000 loops, best of 3: 18.2 us per loop

np.dstack((A,B,C)).swapaxes(1,2)
100000 loops, best of 3: 19.8 us per loop

np.hstack([A,B,C]).reshape((5,3,3))
100000 loops, best of 3: 14.8 us per loop

np.hstack([A[:, None, :], B[:, None, :], C[:, None, :]])
100000 loops, best of 3: 17.2 us per loop

看起来@Viktor Kerkez 的回答是最快的。

于 2013-09-03T15:56:28.630 回答
2

我想我得到了一些有用的东西:

>>> print np.hstack([A[:, None, :], B[:, None, :], C[:, None, :]])
[[[ 1  0  0]
  [ 5  9  9]
  [ 0  0  0]]

 [[ 3  0  0]
  [37  8  9]
  [ 0  6  0]]

 [[ 5  2  0]
  [49  8  3]
  [ 1  4  6]]

 [[ 2  0  0]
  [ 3  3  1]
  [ 6  2  0]]

 [[ 1  2  1]
  [ 4  4  5]
  [ 0  5  4]]]
于 2013-09-03T15:05:59.377 回答