numpy.linspace详解
生活随笔
收集整理的這篇文章主要介紹了
numpy.linspace详解
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
numpy.linspace
-
用例:
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) -
功能:
在指定區(qū)間內(nèi)返回均勻間隔的數(shù)字。
返回區(qū)間[start, stop]范圍內(nèi)num個(gè)均勻間隔的樣本。
可以選擇是否包含區(qū)間的結(jié)束位置點(diǎn)。 -
參數(shù):
| start | 標(biāo)量 | 序列起始位置的值 |
| stop | 標(biāo)量 | 當(dāng)endpoint=True時(shí),代表序列結(jié)束位置的值。當(dāng)endpoint=False時(shí),序列會(huì)從[start, stop-1]范圍內(nèi)均勻間隔采集num個(gè)樣本。注意:當(dāng)endpoint=False時(shí),采集的間隔會(huì)發(fā)生改變 |
| num | 整數(shù),可選參數(shù) | 產(chǎn)生的樣本數(shù)量。默認(rèn)是50。該值必須非負(fù) |
| endpoint | bool變量,可選參數(shù) | 如果為T(mén)rue,stop是最后一個(gè)值。否則,stop不被包含在序列內(nèi)。默認(rèn)是True |
| retstep | bool變量,可選參數(shù) | 如果為T(mén)rue,返回(samples, step),其中step是樣本之間的采樣間隔 |
| dtype | dtype變量,可選參數(shù) | 返回?cái)?shù)組的數(shù)值類型,如果不給定dtype,從其他輸入?yún)?shù)推斷數(shù)據(jù)類型,這個(gè)是numpy 1.9.0版本的新特性 |
- 返回值:
| samples | n維數(shù)組 | 由閉區(qū)間[start, stop]或半閉區(qū)間[start, stop)內(nèi)num個(gè)等間隔樣本組成。(取決于endpoint是Tru或者False) |
| step | 浮點(diǎn)型數(shù)字,可選參數(shù) | 當(dāng)retstep為T(mén)rue時(shí)才會(huì)返回此參數(shù),代表樣本之間的采樣間隔 |
- 示例:
第一個(gè)輸出為:[2. 2.25 2.5 2.75 3. ]
第二個(gè)輸出為:[2. 2.2 2.4 2.6 2.8]
第三個(gè)輸出為:(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
可視化分析
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()
github鏈接
https://github.com/wzy6642/numpy-translate
總結(jié)
以上是生活随笔為你收集整理的numpy.linspace详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: numpy.random.normal详
- 下一篇: numpy.zeros详解