Python 为什么我的NumPy日志空间给我一个无限数组?

Python 为什么我的NumPy日志空间给我一个无限数组?,python,numpy,Python,Numpy,为了得到一个1000到100000000的对数数组,其中包含23个点,我用Python编写了以下代码: import numpy as np x4 = np.logspace(start=1000, stop=1000000000, num=23, base=10) print(x4) 结果如下: [inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf] 你如何

为了得到一个1000到100000000的对数数组,其中包含23个点,我用Python编写了以下代码:

import numpy as np

x4 = np.logspace(start=1000, stop=1000000000, num=23, base=10)
print(x4)
结果如下:

[inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf inf]
你如何解决这个问题?我在代码中做错了什么?

不是在做你认为它在做的事情。您预期的效果是:

事实上,你正在

10**np.linspace(1000, 1000000000, 23)
从文档中:

在线性空间中,序列开始于
base**start
(以start的幂为基数),结束于
base**stop
(参见下面的端点)

所以你可能想要

np.logspace(3, 9, num=23, base=10)
或者

np.geomspace(10**3, 10**9, 23)
产生这种结果的确切原因可以从以下几点看出:


由于
10**1000>1.7976931348623157e+308
inf
只是预期溢出的信号。

我认为您误解了logspace的功能。它给你力量,从
base**start
开始,在你的例子中是
10**1000
。因此,在你的情况下:

out = np.logspace(start=3, stop=9, num=23, base=10)
和测试:

plt.plot(out)
plt.yscale('log')
输出:


这更有意义,谢谢!那就更有意义了,谢谢!你现在有了足够的声望去投票,这是一种“正确”的感谢方式。此外,您还可以通过单击答案旁边的复选标记来选择答案。这将从未回答的队列中删除您的问题。欢迎来到SO,祝贺我假设这是一个成功的第一个问题!您可以使用
np.geomspace(start=1000,stop=100000000,num=23),而不是
np.logspace
out = np.logspace(start=3, stop=9, num=23, base=10)
plt.plot(out)
plt.yscale('log')