numpy np.expand_dims和np.squeeze(增减维度)

增加维度

import numpy as np

a = np.array([[1,2],[3,4]])

b=np.expand_dims(a,axis=0)

print(b)
print(b.shape)

执行结果:

array([[[1, 2],
        [3, 4]]])

(1, 2, 2)

在0轴上增加1个维度

减少维度

删除是1的维度

c=np.squeeze(b)

print(c)
print(c.shape)

执行结果:

[[1 2]
 [3 4]]

(2, 2)

删除是1的维度


原文出处:/show_1EF5172dkVGn.html