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)
本文通过实例展示了如何使用NumPy的np.newaxis来为一维数组增加维度,包括将一维数组转换为行向量和列向量,并介绍了转换前后数组形状的变化。
171

被折叠的 条评论
为什么被折叠?



