我必须从两个值(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.]]])
如何获得预期的数组?
答案 0 :(得分:1)
您可以将合并的np.tile
与np.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.]]])