import numpy as np
In [30]:
#np.newaxis多用于防止取出一行或列后数据降维
a = np.arange(6).reshape(2,3);
a
Out[30]:
In [31]:
# np.newaxis加在哪个位置,就能在shape里看到相应位置增加了一个纬度
c = a[:, np.newaxis] #这里相当于a[:, np.newaxis, :]加在了行的后面列的前面
print(c)
print("c.shape",c.shape)
In [27]:
d = a[:,np.newaxis, 2] #这里是取2号列
print(d)
print("d.shape",d.shape)
In [25]:
e = a[1, np.newaxis, :] #这里是取1号行
print(e)
print("e.shape",e.shape)