gpt4 book ai didi

python - 一起使用 mpmath 和 scipy.stat

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:23 24 4
gpt4 key购买 nike

我正在尝试同时使用提供任意精度算术的 mpmath 库和 scipy.stats 库:

from mpmath import mpf
from scipy.stats import norm

x = mpf(3) # arbitrary precision float
y = norm.cdf(x)

但是,norm.cdf 通过调用 np.isnan(x) 在内部检查其输入是否为数字。因此,我收到以下错误:

Traceback (most recent call last):

File "name of my file", line 5, in <module>
y = norm.cdf(x)

File "C:\Program Files\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 1734, in cdf
place(output, (1-cond0)+np.isnan(x), self.badvalue)

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

有没有办法强制scipy.stats.cdf使用mpmath.isnan而不是np.isnan?或者有其他方法可以解决这个问题吗?

最佳答案

mpmath 实现了自己的正态分布方法:mpdf and ncdf

from mpmath import ncdf
y = ncdf(x) # returns mpf('0.9986501019683699')

除了将 mpf 向下转换为常规 float 之外,您无法使非 mpmath 方法与 mpf 对象一起使用。他们的底层计算例程被设计为以固定精度工作(通常在 Fortran 中),并且不知道如何处理 mpf。这就是 mpmath 重新实现 SciPy 中已存在的数学函数的原因。

关于python - 一起使用 mpmath 和 scipy.stat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41707018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com