numpy.tile为3d数组

时间:2017-10-14 20:15:05

标签: python arrays numpy

我必须从两个值(60,80)获得3D数组维数(4,2,1)

import numpy as np
array([[[ 60.],
        [ 60.]],
   [[ 60.],
    [ 60.]],
   [[ 80.],
    [ 80.]],
   [[ 80.],
    [ 80.]]])

我做如下

a = np.array([[[ 60.]],
       [[ 80.]]])
np.tile(a, (2, 2,1))

并获取

array([[[ 60.],
        [ 60.]],
       [[ 80.],
        [ 80.]],
       [[ 60.],
        [ 60.]],
       [[ 80.],
        [ 80.]]])

如何获得预期的数组?

1 个答案:

答案 0 :(得分:1)

您可以将合并的np.tilenp.repeat

结合使用
a = np.array([[[ 60.]],
       [[ 80.]]])
np.repeat(np.tile(a, (1,2,1)), 2, axis=0)

#array([[[ 60.],
#        [ 60.]],

#       [[ 60.],
#        [ 60.]],

#       [[ 80.],
#        [ 80.]],

#       [[ 80.],
#        [ 80.]]])

或使用np.repeat + reshape

np.repeat(a, 4).reshape(4,2,1)
#array([[[ 60.],
#        [ 60.]],

#       [[ 60.],
#        [ 60.]],

#       [[ 80.],
#        [ 80.]],

#       [[ 80.],
#        [ 80.]]])