python numpy.linspace() 使用介绍
生活随笔
收集整理的這篇文章主要介紹了
python numpy.linspace() 使用介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
numpy.linspace()
在指定的間隔內返回均勻間隔的數字。
示例
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# 在閉區間[2, 3]生成5個間隔相同的數字 print(np.linspace(2.0, 3.0, num=5)) # [2. 2.25 2.5 2.75 3. ]# 在半開區間[2, 3)生成5個間隔相同的數字 print(np.linspace(2.0, 3.0, num=5, endpoint=False)) # [2. 2.2 2.4 2.6 2.8]# 在閉區間[2, 3]生成5個間隔相同的數字(除了返回生成的樣本數字,還返回樣本數字之間的間距) 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.返回指定區間內的均勻間隔的數字。返回在區間[`start`,`stop`]中計算出的num個均勻間隔的樣本。區間的端點可以選擇排除。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.序列的結束值,除非將“ endpoint”設置為False。在這種情況下,該序列由除num + 1之外的所有其他均勻間隔的樣本組成,因此排除了stop。 注意,當`endpoint`為False時步長會改變。num : int, optionalNumber of samples to generate. Default is 50. Must be non-negative.要生成的樣本數。 默認值為50。必須為非負數。endpoint : bool, optionalIf True, `stop` is the last sample. Otherwise, it is not included.Default is True.如果為True,則“ stop”是最后一個樣本。 否則,不包括在內。默認值為True。retstep : bool, optionalIf True, return (`samples`, `step`), where `step` is the spacingbetween samples.如果為True,則返回(“ samples”,“ step”),其中“ step”是樣本之間的間距。dtype : dtype, optionalThe type of the output array. If `dtype` is not given, infer the datatype from the other input arguments.輸出數組的類型。 如果未給出dtype,則從其他輸入參數推斷數據類型。.. 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).在閉區間“ [start,stop]”或半開區間“ [start,stop)”中有“ num”個等距樣本(取決于“ endpoint”是True還是False)。step : float, optionalOnly returned if `retstep` is True僅在`retstep`為True時返回Size of spacing between samples.樣本之間的間距大小。See Also--------arange : Similar to `linspace`, but uses a step size (instead of thenumber of samples).類似于linspace,但是使用步長(而不是樣本數)。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:圖形說明:>>> 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小時Python零基礎輕松入門
總結
以上是生活随笔為你收集整理的python numpy.linspace() 使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 机器学习中,clf变量代表
- 下一篇: python numpy中arange(