numpy.linspace和numpy.newaxis

>>> import numpy as np

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

在指定的间隔内返回均匀间隔的数字。在[start, stop]这个区间的端点可以任意的被排除在外,默认包含端点;retstep=True时,显示间隔长度。

>>> np.linspace(2.0,3.0,num=5)
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>> np.linspace(2.0,3.0,num=5,endpoint=False)
array([2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0,3.0,num=5,retstep=True)
(array([2.  , 2.25, 2.5 , 2.75, 3.  ]), 0.25)

numpy.newaxis None的方便别名,对于索引数组是有用的。

>>> np.newaxis is None
True
>>> x = np.array([1,2,3]) # 一维数组[1,2,3]
>>> x
array([1, 2, 3])
>>> x.shape
(3,)
>>> x[:,np.newaxis]# 二维数组
array([[1],
       [2],
       [3]])
>>> x[:,np.newaxis].shape
(3, 1)
>>> x[np.newaxis,:]# 二维数组
array([[1, 2, 3]])
>>> x[np.newaxis,:].shape
(1, 3)
>>> x[:,np.newaxis,np.newaxis]# 三维数组
array([[[1]],

       [[2]],

       [[3]]])
>>> x[:,np.newaxis,np.newaxis].shape
(3, 1, 1)

猜你喜欢

转载自blog.csdn.net/Threelights/article/details/84880503