numpy生成等差等比数列

本文介绍了numpy库中用于生成等差数列的arange函数,以及linspace和logspace函数的用法和参数。通过实例演示了如何创建不同类型的数列,如等距、等比的数列,适合理解数组操作的基本工具。
摘要由CSDN通过智能技术生成

文章目录

arange

numpy.arange([start ], stop[, step ], dtype=None)

功能:

Return evenly spaced values within a given interval.
返回一个[start,stop]区间内步长为step的均匀间隔的数列(等差数列)

参数:

start: 数列起点,默认为0
stop: 数列终点,输出序列不包括这一点
step: 步长(即公差),默认为1
dtype: 数据类型

输出序列长度 ceil((stop - start)/step)

示例:

>>> np.arange(3)#注意输出结果不包括3
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0., 1., 2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])

linspace

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
功能:

返回一个[start,stop]区间内长度为num的均匀间隔的数列(等差数列). 当endpoint=True时,间距=(stop-start)/(num-1),数列包含stop。当endpoint=False时,间距=(stop-start)/num,数列不包含stop.

参数:

start [array_like] 数列的起点.
stop [array_like] 数列的终点
num [int, optional] 数列长度.
endpoint [bool, optional]
endpoint=True时,间距=(stop-start)/(num-1),数列包含stop。当endpoint=False时,间距=(stop-start)/num,数列不包含stop.
retstep [bool, optional] 如果True, 结果还会返回数字之间的间距.
dtype [dtype, optional] 输出的数据类型.
axis [int, optional] (一般用不到)The axis in the result to store the samples. Relevant only if start or stop are array-like. By default (0), the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end.

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

logspace

numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)

功能:

base默认为10,返回从的 ( b a s e ) s t a r t (base)^{start} (base)start ( b a s e ) s t o p (base)^{stop} (base)stop之间的指数呈等差数列,长度为num的数列

参数的意义请参考linspace函数的相应介绍

>>> np.logspace(2.0, 3.0, num=4)
array([ 100. , 215.443469 , 464.15888336, 1000. ])
>>> np.logspace(2.0, 3.0, num=4, endpoint=False)
array([100. , 177.827941 , 316.22776602, 562.34132519])
>>> np.logspace(2.0, 3.0, num=4, base=2.0)
array([4. , 5.0396842 , 6.34960421, 8. ])
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值