numpy.exp()溢出 [英] Overflow with numpy.exp()

查看:494
本文介绍了numpy.exp()溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须找到3个具有scipy的minimumsq函数的参数,因为该过程已描述此处.

I have to find 3 parameters with the leastsq function of scipy, as the procedure is described here in the part least-square fitting.

这是我的代码:

def myFunc(t, Vs, Vi, k):
    y = Vs * t - ((Vs - Vi) * (1 - np.exp(-k * t)) / k)
    return y

x = np.array(temps, dtype='float64')
y = np.array(fluo, dtype='float64')

Vs, Vi, k = [2.8707e-11, 0.01241, 19765.39043]

def residuals(p, y, x):
    Vs, Vi, k = p
    err = y - myFunc(x, Vs, Vi, k) 
    return err

def peval(x, p):
    return myFunc(x, p[0], p[1], p[2])

p0 = [2.8707e-11, 0.01241, 19765.39043]
plsq = leastsq(residuals, p0, args=(y, x))

print(plsq[0])
print(np.array([Vs, Vi, k]))

但是我得到警告:

RuntimeWarning: overflow encountered in exp

我认为exp中产生的数字太大而无法容纳在float64中.但是我真的需要使它适应.有什么办法可以解决这个问题?

I assume the number produced in exp is too big to fit in a float64. But I really need to make it fit. Is there any option to overcome this issue?

推荐答案

无法查看输入数据(包含在数组tempsfluo中),很难准确地说出,但是我不知道认为这是值得担心的事情*.为 scipy.optimize.leastsq 工作时,它通过雅可比行列式(Jacobian)优化了一组方程,并逐步向局部最小值行进.在此过程中,某些值可能会溢出,这些值应使用优化程序正确处理.该警告来自对溢出的第一次评估,它可能不止一次发生,但仅是第一次显示.

Without being able to look at the input data (contained in your arrays temps and fluo) it's hard to say exactly, but I don't think this is something to worry about*. As scipy.optimize.leastsq works it optimizes a set of equations via the Jacobian and marches down to a local minimum. Along the way, it is possible that some of the values will overflow, these should be handled correctly with the optimizer. The warning comes from the first evaluation of the overflow, it may be happening more than once but it is only shown for the first time.

*前提是您检查解决方案并确认其物理性和合理性. Minimumsq的输出为您提供了参数估计值的协方差,您可以使用该协方差来确定拟合的进展程度.

* Provided that you check your solution and verify that it is physical and gives a reasonable fit. The output from leastsq gives you a covariance of the parameter estimates which you can use to determine how well the fit progressed.

这篇关于numpy.exp()溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆