文檔地址:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
Parameters(參數):
start : 序列的起始點.
stop : 序列的結束點
num : 生成的樣本數,默認是50。必須是非負。
endpoint : 如果True,'stop'是最后一個樣本。否則,它不包括在內。默認為True。
retstep : 如果True,返回 (`samples`, `step`)
dtype :
第1個例子endpoint的使用:
import numpy as np
print(np.linspace(2.0, 3.0, num=5))
print(np.linspace(2.0, 3.0, num=5, endpoint=True))
print(np.linspace(2.0, 3.0, num=5, endpoint=False))
輸出:
[ 2. 2.25 2.5 2.75 3. ]
[ 2. 2.25 2.5 2.75 3. ]
[ 2. 2.2 2.4 2.6 2.8]
從上面輸出可以看出endpoint=True時,輸出包含了“stop”這個樣本點;endpoint=False時,輸出不包括“stop”這個樣本點;默認情況endpoint=True。
第2個例子retstep的使用:
import numpy as np
print(np.linspace(2.0, 3.0, num=5))
a = np.linspace(2.0, 3.0, num=5, retstep=True)
print(a)
print(a[0])
print(a[1])
輸出:
[ 2. 2.25 2.5 2.75 3. ]
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
[ 2. 2.25 2.5 2.75 3. ]
0.25
retstep=True時輸出了步長(step),此時步長為0.25
第3個例子:
import numpy as np
import matplotlib.pyplot as plt
N = 8
y = np.zeros(N)
x1 = np.linspace(0, 10, N, endpoint=True)
x2 = np.linspace(0, 10, N, endpoint=False)
plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.5, 'o')
plt.ylim([-0.5, 1])
plt.show()
輸出: