有关numpy 中 newaxis函数的使用

函数功能:对数组新加一个维度,为了好理解直接看例子吧:

>>> import numpy as np
>>> #先创建一个一维数组
>>> a=np.array([1,2,3,4])
>>> #看下a数组的shape
>>> print(a.shape)
(4,)
>>> #现在我们想在一维数组a的基础上得到(4,1)的二维数组可利用上述函数
>>> b=a[:,np.newaxis]
>>> print(b.shape)
(4, 1)
>>> #如果我们想得到(1,4)这样的二位数组,只要这样做就可以了
>>> c=a[np.newaxis,:]
>>> print(c)
[[1 2 3 4]]
>>> print(c.shape)
(1, 4)
 

猜你喜欢

转载自blog.csdn.net/qq_29023939/article/details/81041147