numpy中的expand_dims函数

常见的一种应用场景:

条件:假设A的shape为[4, 2],B的shape为[5, 2]

目的:实现A中的每一行, 减去B中的所有行(broadcast操作)。

实现:

A1 =  np.expand_dims(A, -2) => A1的shape变为[4, 1, 2]

B1 = np.expand_dims(B, 0) => B1的shape变为[1, 5, 2]

A1 - B1

------------------------------------------------------------------------------------------------

其他示例:

wh = np.random.randint(1,3, size=(4,2))

np.expand_dims(wh, -2).shape

np.expand_dims(wh, 1).shape

在倒数第2个轴后面(在正数第1个轴后面)插入一个新轴。

-- over --

猜你喜欢

转载自blog.csdn.net/qm5132/article/details/83379171