np.newaxis用法详解

np.newaxis,增加维度

In [1]: np.linspace(1, 10, 10)
Out[1]: array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

In [2]: np.linspace(1, 10, 10)[np.newaxis,:]
Out[2]: array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]])

In [3]: np.linspace(1, 10, 10)[:,np.newaxis]
Out[3]:
array([[  1.],
       [  2.],
       [  3.],
       [  4.],
       [  5.],
       [  6.],
       [  7.],
       [  8.],
       [  9.],
       [ 10.]])

In [4]: np.linspace(1, 10, 10).shape
Out[4]: (10,)

In [5]: np.linspace(1, 10, 10)[np.newaxis,:].shape
Out[5]: (1, 10)

In [6]: np.linspace(1, 10, 10)[:,np.newaxis].shape
Out[6]: (10, 1)
原文地址:https://www.cnblogs.com/twfb/p/7685402.html