原文链接:np.newaxis作用详解—-超简单理解方式,通透

np.newaxis的作用就是在这一位置增加一个一维,这一位置指的是np.newaxis所在的位置,举个例子如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
x1 = np.array([1, 2, 3, 4, 5])
# the shape of x1 is (5,)
x1_new = x1[:, np.newaxis]
# now, the shape of x1_new is (5, 1)
# array([[1],
# [2],
# [3],
# [4],
# [5]])
x1_new = x1[np.newaxis,:]
# now, the shape of x1_new is (1, 5)
# array([[1, 2, 3, 4, 5]])