python numpy 函数详解_numpy.linspace函数具体使用详解

numpy.linspace(start, stop, num=50, endpoint=true, retstep=false, dtype=none)

在指定的间隔内返回均匀间隔的数字。

返回num均匀分布的样本,在[start, stop]。

这个区间的端点可以任意的被排除在外。

parameters(参数):

start : scalar(标量)

the starting value of the sequence(序列的起始点).

stop : scalar

序列的结束点,除非endpoint被设置为false,在这种情况下, the sequence consists of all but the last of num + 1 evenly spaced samples(该序列包括所有除了最后的num+1上均匀分布的样本(感觉这样翻译有点坑)), 以致于stop被排除.当endpoint is false的时候注意步长的大小(下面有例子).

num : int, optional(可选)

生成的样本数,默认是50。必须是非负。

endpoint : bool, optional

如果是真,则一定包括stop,如果为false,一定不会有stop

retstep : bool, optional

if true, return (samples, step), where step is the spacing between samples.(看例子)

dtype : dtype, optional

the type of the output array. if  is not given, infer the data type from the other input arguments(推断这个输入用例从其他的输入中).

new in version 1.9.0.

returns:

samples : ndarray

there 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).

step : float(只有当retstep设置为真的时候才会存在)

only returned if retstep is true

size of spacing between samples.

see also

arange

similar to linspace, but uses a step size (instead of the number of samples)

.arange使用的是步长,而不是样本的数量

logspace

samples uniformly distributed in log space.

当endpoint被设置为false的时候

>>> import numpy as np

>>> np.linspace(1, 10, 10)

array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])

>>> np.linspace(1, 10, 10, endpoint = false)

array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])

in [4]: np.linspace(1, 10, 10, endpoint = false, retstep= true)

out[4]: (array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1]), 0.9)

官网的例子

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')

[]

>>> plt.plot(x2, y + 0.5, 'o')

[]

>>> plt.ylim([-0.5, 1])

(-0.5, 1)

>>> plt.show()

b_0_201906022356239183.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值