官方文檔: numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
- 在指定的間隔內(nèi)返回均勻間隔的數(shù)字;
- 返回num均勻間隔的樣本, 在
[start,stop]區(qū)間內(nèi)計(jì)算; - 可以選擇排除間隔的終點(diǎn).
Parameters
- start: scalar(標(biāo)量), 序列的起始點(diǎn)
- stop: scalar, 依據(jù)endpoint會(huì)有變化, endpoint為T(mén)rue, 則包含顯示, 當(dāng)為False, 不包含(生成序列相當(dāng)于原始num上加1按endpoint = True生成, 結(jié)果只顯示第一個(gè)到倒數(shù)第二個(gè))
- num: int, optional(可選), 生成樣本數(shù)量, 必須是非負(fù)數(shù)
- endpoint: bool, optional, 如果是真,則包括stop,如果為False,則沒(méi)有stop
- retstep: bool, optional
- dtype: dtype, optional
Returns
- samples : ndarray
- step : float, optional
Examples
import numpy as np
np.linspace(-10, 10, 11)
# array([-10., -8., -6., -4., -2., 0., 2., 4., 6., 8., 10.])
import matplotlib.pyplot as plt
num = 11
y = np.zeros(num)
endpoint_T = np.linspace(-10, 10, num, endpoint = True)
endpoint_F = np.linspace(-10, 10, num, endpoint = False)
plt.plot(endpoint_T, y, 'o')
plt.plot(endpoint_F, y + 0.5, 'o')
plt.ylim([-0.5, 1])
xticks(np.linspace(-10, 10, num, endpoint = True))
plt.show()
# 設(shè)置dtype
lin_dtype = np.linspace(-10, 10, 11, dtype = np.int)
lin_dtype
# array([-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10])
print(lin_dtype.dtype) # int64
