Python – numpy.linspace()

Being a sequence generator, the numpy.linspace() function is used to generate a sequence of numbers in linear space with a uniform step size.

作为序列生成器, numpy.linspace()函数用于在线性空间中以均匀步长生成数字序列。

Numpy generally can generate sequences using numpy.arange(), but when we use floating-point arguments, it can result in a loss of precision, which may cause an unpredictable output.

Numpy通常可以使用numpy.arange()生成序列,但是当我们使用浮点参数时,可能会导致精度损失,这可能会导致不可预测的输出。

To avoid any potential loss of precision due to the floating-point precision, numpy provides us with a separate sequence generator in numpy.linspace(), which is the preferred option if you already know the number of elements you need. But you generally get the same output using both linspace() and arange() with appropriate parameters, so both can be chosen for the same task.

为了避免由于浮点精度而造成的任何精度损失,numpy在numpy.linspace()为我们提供了一个单独的序列生成器,如果您已经知道所需的元素数,则这是首选。 但是通常使用带有适当参数的linspace()arange()可以得到相同的输出,因此可以为同一任务选择两者。

For example, the following code plots 2 linear sequences between 0 and 10 using numpy.linspace() to show that there is uniformity generated by the sequence.

例如,以下代码使用numpy.linspace()在0到10之间绘制2个线性序列,以显示该序列生成的均匀性。


import numpy as np
import matplotlib.pyplot as plt

y = np.zeros(5)
x1 = np.linspace(0, 10, 5)
x2 = np.linspace(0, 10, 5)
plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.5, 'o')
plt.ylim([-0.5, 1])
plt.show()

Output:

输出

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值