python numpy.linspace() 使用介绍
生活随笔
收集整理的這篇文章主要介紹了
python numpy.linspace() 使用介绍
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
numpy.linspace()
在指定的間隔內(nèi)返回均勻間隔的數(shù)字。
示例
1)
# -*- coding: utf-8 -*- """ @File : test.py @Time : 2020/4/1 19:24 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import numpy as np# 在閉區(qū)間[2, 3]生成5個(gè)間隔相同的數(shù)字 print(np.linspace(2.0, 3.0, num=5)) # [2. 2.25 2.5 2.75 3. ]# 在半開(kāi)區(qū)間[2, 3)生成5個(gè)間隔相同的數(shù)字 print(np.linspace(2.0, 3.0, num=5, endpoint=False)) # [2. 2.2 2.4 2.6 2.8]# 在閉區(qū)間[2, 3]生成5個(gè)間隔相同的數(shù)字(除了返回生成的樣本數(shù)字,還返回樣本數(shù)字之間的間距) print(np.linspace(2.0, 3.0, num=5, retstep=True)) # (array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)2)
官方doc
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):"""Return evenly spaced numbers over a specified interval.Returns `num` evenly spaced samples, calculated over theinterval [`start`, `stop`].The endpoint of the interval can optionally be excluded.返回指定區(qū)間內(nèi)的均勻間隔的數(shù)字。返回在區(qū)間[`start`,`stop`]中計(jì)算出的num個(gè)均勻間隔的樣本。區(qū)間的端點(diǎn)可以選擇排除。Parameters----------start : scalarThe starting value of the sequence.序列的起始值。stop : scalarThe end value of the sequence, unless `endpoint` is set to False.In that case, the sequence consists of all but the last of ``num + 1``evenly spaced samples, so that `stop` is excluded. Note that the stepsize changes when `endpoint` is False.序列的結(jié)束值,除非將“ endpoint”設(shè)置為False。在這種情況下,該序列由除num + 1之外的所有其他均勻間隔的樣本組成,因此排除了stop。 注意,當(dāng)`endpoint`為False時(shí)步長(zhǎng)會(huì)改變。num : int, optionalNumber of samples to generate. Default is 50. Must be non-negative.要生成的樣本數(shù)。 默認(rèn)值為50。必須為非負(fù)數(shù)。endpoint : bool, optionalIf True, `stop` is the last sample. Otherwise, it is not included.Default is True.如果為T(mén)rue,則“ stop”是最后一個(gè)樣本。 否則,不包括在內(nèi)。默認(rèn)值為T(mén)rue。retstep : bool, optionalIf True, return (`samples`, `step`), where `step` is the spacingbetween samples.如果為T(mén)rue,則返回(“ samples”,“ step”),其中“ step”是樣本之間的間距。dtype : dtype, optionalThe type of the output array. If `dtype` is not given, infer the datatype from the other input arguments.輸出數(shù)組的類(lèi)型。 如果未給出dtype,則從其他輸入?yún)?shù)推斷數(shù)據(jù)類(lèi)型。.. versionadded:: 1.9.0Returns-------samples : ndarrayThere are `num` equally spaced samples in the closed interval``[start, stop]`` or the half-open interval ``[start, stop)``(depending on whether `endpoint` is True or False).在閉區(qū)間“ [start,stop]”或半開(kāi)區(qū)間“ [start,stop)”中有“ num”個(gè)等距樣本(取決于“ endpoint”是True還是False)。step : float, optionalOnly returned if `retstep` is True僅在`retstep`為T(mén)rue時(shí)返回Size of spacing between samples.樣本之間的間距大小。See Also--------arange : Similar to `linspace`, but uses a step size (instead of thenumber of samples).類(lèi)似于linspace,但是使用步長(zhǎng)(而不是樣本數(shù))。logspace : Samples uniformly distributed in log space.樣本在日志空間中均勻分布。Examples-------->>> 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)Graphical illustration:圖形說(shuō)明:>>> 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')[<matplotlib.lines.Line2D object at 0x...>]>>> plt.plot(x2, y + 0.5, 'o')[<matplotlib.lines.Line2D object at 0x...>]>>> plt.ylim([-0.5, 1])(-0.5, 1)>>> plt.show()"""參考文章:numpy.linspace使用詳解
8小時(shí)Python零基礎(chǔ)輕松入門(mén)
總結(jié)
以上是生活随笔為你收集整理的python numpy.linspace() 使用介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 机器学习中,clf变量代表
- 下一篇: python numpy中arange(