numpy exp中旧代码中的新错误 [英] new error in old code in numpy exp

查看:91
本文介绍了numpy exp中旧代码中的新错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在处理一些数据,在保存完绘图后可以使用curve_fit获得一条曲线,并且获得的值后来又返回到相同的代码,只是发现它不起作用.

Recently I was working on some data for which I was able to obtain a curve using curve_fit after saving the plot and the values obtained I returned to the same code later only to find it does not work.

#! python 3.5.2

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
from scipy.optimize import curve_fit

data= np.array([
    [24, 0.176644513],
    [27, 0.146382841],
    [30, 0.129891534],
    [33, 0.105370908],
    [38, 0.077820511],
    [50, 0.047407538]])
x, y = np.array([]), np.array([])

for val in data:
    x = np.append(x, val[0])
    y = np.append(y, (val[1]/(1-val[1])))

def f(x, a, b): 
    return (np.exp(-a*x)**b)

# The original a and b values obtained
a = -0.2 # after rounding
b = -0.32 # after rounding
plt.scatter(x, y)
Xcurve = np.linspace(x[0], x[-1], 500)
plt.plot(Xcurve, f(Xcurve,a,b), ls='--', color='k', lw=1)
plt.show()

# the original code to get the values
a = b = 1
popt, pcov = curve_fit(f, x, y, (a, b))

在此之前,curve_fit返回值a,b = -0.2,-0.32现在返回: 警告(来自警告模块): 文件"C:/用户...第22行 返回(np.exp(-a * x)** b) RuntimeWarning:exp中遇到溢出

Whereas, previously curve_fit returned the values a, b = -0.2, -0.32 now returns: Warning (from warnings module): File "C:/Users ... line 22 return (np.exp(-a*x)**b) RuntimeWarning: overflow encountered in exp

据我所知,代码没有更改.谢谢

The code as far as I am aware did not change. Thanks

推荐答案

在不知道代码中发生了什么变化的情况下,很难说出工作"状态和不工作"状态之间的变化.您使用的scipy版本的更改可能会得出不同的结果:在过去的几年中,curve_fit()中的基础实现发生了更改.

Without knowing what changed in the code, it is hard to say what changed between your state of "working" and "not working". It may be that changes in the version of scipy you used give different results: there have changes to the underlying implementation in curve_fit() over the past few years.

而且:curve_fit()(以及它使用的基础python和Fortran代码)要求对参数进行合理的良好初始猜测,才能使许多问题完全起作用.如果对参数有错误的猜测,许多问题将失败.

But also: curve_fit() (and the underlying python and Fortran code it uses) requires reasonably good initial guesses for the parameters for many problems to work at all. With bad guesses for the parameters, many problems will fail.

对于Levenberg-Marquardt算法(以及curve_fit()所使用的实现),指数衰减问题似乎特别具有挑战性,并且确实需要合理的起点.也很容易进入参数空间的一部分,函数将其计算为零,并且参数值的更改无效.

Exponential decay problems seem to be especially challenging for the Levenberg-Marquardt algorithm (and the implementations used by curve_fit(), and do require reasonable starting points. It's also easy to get into a part of parameter space where the function evaluates to zero, and changes in the parameter values have no effect.

如果可能,如果您的问题涉及指数衰减,那么在日志空间中工作将很有帮助.也就是说,模型log(f)而不是f本身.特别是对于您的问题,您的模型函数为exp(-a*x)**b.那真的是你的意思吗? ab将完全相关.

If possible, if your problem involves exponential decay, it is helpful to work in log space. That is, model log(f), not f itself. For your problem in particular, your model function is exp(-a*x)**b. Is that really what you mean? a and bwill be exactly correlated.

此外,您可能会发现lmfit有用.它具有使用相似的基础代码进行曲线拟合的Model类,但允许固定或设置任何参数的边界.您的问题的一个示例是(大约):

In addition, you may find lmfit helpful. It has a Model class for curve-fitting, using similar underlying code, but allows fixing or setting bounds on any of the parameters. An example for your problem would be (approximately):

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
from scipy.optimize import curve_fit

import lmfit

data= np.array([
    [24, 0.176644513],
    [27, 0.146382841],
    [30, 0.129891534],
    [33, 0.105370908],
    [38, 0.077820511],
    [50, 0.047407538]])
x, y = np.array([]), np.array([])

for val in data:
    x = np.append(x, val[0])
    y = np.append(y, (val[1]/(1-val[1])))

def f(x, a, b):
    print("In f:  a, b =  " , a, b)
    return (np.exp(-a*x)**b)


fmod = lmfit.Model(f)
params = fmod.make_params(a=-0.2, b=-0.4)

# set bounds on parameters
params['a'].min = -2
params['a'].max =  0
params['b'].vary = False

out = fmod.fit(y, params, x=x)
print(out.fit_report())

plt.plot(x, y)
plt.plot(x, out.best_fit, '--')
plt.show()

这篇关于numpy exp中旧代码中的新错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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