newaxis的作用为给numpy数组增加新的维度
示例代码:
import numpy as np
a = np.array([1,2,3]) #a.shsape=(3,)
a1 = a[np.newaxis,:] #a1.shape=(1,3)
print(a1) #[[1 2 3]]
a2 = a[:,np.newaxis] #a2.shape=(3,1)
print(a2)
打印结果:
[[1 2 3]]
[[1]
[2]
[3]]
总结:np.newaxis放在哪个维度上,那个维度就增加1