ValueError:输入包含nan值-来自lmfit模型的输入,尽管输入不包含NaN [英] ValueError: The input contains nan values - from lmfit model despite the input not containing NaNs

查看:172
本文介绍了ValueError:输入包含nan值-来自lmfit模型的输入,尽管输入不包含NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lmfit (链接 ,并且似乎无法找出为什么我在尝试拟合模型时总是得到ValueError: The input contains nan values的原因.

I'm trying to build a model using lmfit (link to docs) and I can't seems to find out why I keep getting a ValueError: The input contains nan values when I try to fit the model.

from lmfit import minimize, Minimizer, Parameters, Parameter, report_fit, Model
import numpy as np

def cde(t, Qi, at, vw, R, rhob_cb, al, d, r):
    # t (time), is the independent variable
    return Qi / (8 * np.pi * ((at * vw)/R) * t * rhob_cb * (np.sqrt(np.pi * ((al * vw)/R * t))))  * \
        np.exp(- (R * (d - (t * vw)/ R)**2) / (4 * (al * vw) * t) - (R * r**2)/ (4 * (at * vw) * t))

model_cde =  Model(cde)


# Allowed to vary
model_cde.set_param_hint('vw', value =10**-4, min=0.000001)
model_cde.set_param_hint('d', value = -0.038, min = 0.0001)
model_cde.set_param_hint('r', value = 5.637e-10)
model_cde.set_param_hint('at', value =0.1)
model_cde.set_param_hint('al', value =0.15)

# Fixed
model_cde.set_param_hint('Qi', value = 1000, vary = False)
model_cde.set_param_hint('R', value =1.7, vary = False)
model_cde.set_param_hint('rhob_cb', value =3000, vary = False)

# test data
data = [ 1.37,  1.51,  1.65,  1.79,  1.91,  2.02,  2.12,  2.2 ,
        2.27,  2.32,  2.36,  2.38,  2.4 ,  2.41,  2.42,  2.41,  2.4 ,
        2.39,  2.37,  2.35,  2.33,  2.31,  2.29,  2.26,  2.23,  2.2 ,
        2.17,  2.14,  2.11,  2.08,  2.06,  2.02,  1.99,  1.97,  1.94,
        1.91,  1.88,  1.85,  1.83,  1.8 ,  1.78,  1.75,  1.72,  1.7 ,
        1.68,  1.65,  1.63,  1.61,  1.58]

time = list(range(5,250,5))

model_cde.fit(data, t= time)

产生以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-785fcc6a994b> in <module>()
----> 1 model_cde.fit(data, t= time)

/home/bprodz/.virtualenvs/phd_dev/lib/python3.5/site-packages/lmfit/model.py in fit(self, data, params, weights, method, iter_cb, scale_covar, verbose, fit_kws, **kwargs)
    539                              scale_covar=scale_covar, fcn_kws=kwargs,
    540                              **fit_kws)
--> 541         output.fit(data=data, weights=weights)
    542         output.components = self.components
    543         return output

/home/bprodz/.virtualenvs/phd_dev/lib/python3.5/site-packages/lmfit/model.py in fit(self, data, params, weights, method, **kwargs)
    745         self.init_fit    = self.model.eval(params=self.params, **self.userkws)
    746 
--> 747         _ret = self.minimize(method=self.method)
    748 
    749         for attr in dir(_ret):

/home/bprodz/.virtualenvs/phd_dev/lib/python3.5/site-packages/lmfit/minimizer.py in minimize(self, method, params, **kws)
   1240                     val.lower().startswith(user_method)):
   1241                     kwargs['method'] = val
-> 1242         return function(**kwargs)
   1243 
   1244 

/home/bprodz/.virtualenvs/phd_dev/lib/python3.5/site-packages/lmfit/minimizer.py in leastsq(self, params, **kws)
   1070         np.seterr(all='ignore')
   1071 
-> 1072         lsout = scipy_leastsq(self.__residual, vars, **lskws)
   1073         _best, _cov, infodict, errmsg, ier = lsout
   1074         result.aborted = self._abort

/home/bprodz/.virtualenvs/phd_dev/lib/python3.5/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    385             maxfev = 200*(n + 1)
    386         retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
--> 387                                  gtol, maxfev, epsfcn, factor, diag)
    388     else:
    389         if col_deriv:

/home/bprodz/.virtualenvs/phd_dev/lib/python3.5/site-packages/lmfit/minimizer.py in __residual(self, fvars, apply_bounds_transformation)
    369 
    370         out = self.userfcn(params, *self.userargs, **self.userkws)
--> 371         out = _nan_policy(out, nan_policy=self.nan_policy)
    372 
    373         if callable(self.iter_cb):

/home/bprodz/.virtualenvs/phd_dev/lib/python3.5/site-packages/lmfit/minimizer.py in _nan_policy(a, nan_policy, handle_inf)
   1430 
   1431         if contains_nan:
-> 1432             raise ValueError("The input contains nan values")
   1433         return a
   1434 

ValueError: The input contains nan values

但是,以下检查NaN的结果证实我的数据中没有NaN值:

However the results of the following checks for NaNs confirms that there were no NaN values in my data:

print(np.any(np.isnan(data)), np.any(np.isnan(time)))
False False

到目前为止,我已经尝试将列表中的1和/或datatime都转换为numpy ndarrays,删除了第0步(如果存在被0除的错误),显式指定了t是独立的,并且允许所有变量都可以变化.但是,这些都引发相同的错误.

So far I've tried converting 1 and/or both of data and time from lists to numpy ndarrays, removing the 0th time step (in case there was a dividing by 0 error), explicitly specifying the t as being independent and allowing all variables to vary. However these all throw the same error.

是否有人知道导致此错误的原因是什么?谢谢.

Does anyone have ideas what is causing this error to be thrown? Thanks.

推荐答案

我尝试使用scipy.optimize.curve_fit拟合模型并出现以下错误:

I tried to fit my model using scipy.optimize.curve_fit and got the following error:

/home/bprodz/.virtualenvs/phd_dev/lib/python3.4/site-packages/ipykernel/__main__.py:3: RuntimeWarning: invalid value encountered in sqrt
  app.launch_new_instance()

这表明问题在于我的模型为np.sqrt()生成了一些负数.给定负数时,np.sqrt()的默认行为是输出nan 来源

Which suggests the problem is with my model generating some negative numbers for np.sqrt(). The default behaviour for np.sqrt() when given a negative number is to output nan as per this question. NB the np.sqrt can be set to raise an error if given a negative number be setting the following: np.seterr(all='raise') source

提示我也向lmfit google组寻求帮助,并收到以下

TIP I also asked for help in the lmfit google group and received the following helpful advice:

  • 考虑将较长的公式分解成较小的部分,以使故障排除更加容易
  • 使用Model.eval()测试通过模型函数运行时将产生哪些特定参数
  • 在这些(数字)情况下,
  • np.ndarray通常优于python列表
  • Consider breaking long formulas into smaller pieces to make troubleshooting easier
  • Use Model.eval() to test what certain parameters will produce when run through your model function
  • np.ndarray is generally superior to python lists in these (numerical) situations

这篇关于ValueError:输入包含nan值-来自lmfit模型的输入,尽管输入不包含NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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