[英]Unable to fit custom model with lmfit - ValueError: The model function generated NaN values and the fit aborted
I have this data:我有这个数据:
y=[2.103402,2.426855,1.011672,1.595371,1.861879,2.492542,2.567561,4.685010,4.452643,5.321630,6.637233,
6.109260,6.220958,5.928408,5.654726,5.498096,5.468448,6.128418,6.071376,6.487270,6.609533,6.907320,
7.626838,8.432065,9.749410,8.976752,8.742036,8.779956,8.212357,8.578200,9.170012,9.134267,9.199465,
9.094945,9.342948,9.802524,10.959913,10.488497,10.892593,10.673570,10.608582,10.036824,9.741473]
x=[300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300,
2400,2500,2600,2700,2800,2900,3000,3100,3200,3300,3400,3500,3600,3700,3800,3900,4000,4100,4200,4300,4400,4500]
data looks like this, the fit is manually adjusted:数据看起来像这样,拟合是手动调整的:
I want to fit this custom log function:我想适应这个自定义日志 function:
def log_n_func(x, a, b, c, n):
return a*(np.log(b+x)/np.log(n))+c
I tried two approaches:我尝试了两种方法:
import lmfit
def log_n_func(x, a, b, c, n):
return a*(np.log(b+x)/np.log(n))+c
regressor = lmfit.Model(log_n_func)
initial_guess = dict(a=3.61, b=443.86, c=-34, n=2)
results = regressor.fit(data=y, x=x, **initial_guess)
y_fit = results.best_fit
and和
from lmfit import Model, Parameters
model = Model(log_n_func, independent_vars=['x'], param_names=["a", "b", "c", "n"])
params = Parameters()
params.add("a", value=3.6)
params.add("b", value=440)
params.add("c", value=-34)
params.add("n", value=2)
result = model.fit(data=y, params=params, x=x)
but both lead to the same error: ValueError: The model function generated NaN values and the fit aborted. Please check your model function and/or set boundaries on parameters where applicable, In cases like this. using "nan_policy='omit'" will probably not work.
但两者都会导致相同的错误: ValueError: The model function generated NaN values and the fit aborted. Please check your model function and/or set boundaries on parameters where applicable, In cases like this. using "nan_policy='omit'" will probably not work.
ValueError: The model function generated NaN values and the fit aborted. Please check your model function and/or set boundaries on parameters where applicable, In cases like this. using "nan_policy='omit'" will probably not work.
What did I do wrong?我做错什么了?
logarithms are not defined values equal to or below 0, so that numpy log(x)
will sensibly give NaN
for np.log(x)
for x<0
.对数不是等于或小于 0 的定义值,因此 numpy log(x)
将明智地为np.log(x)
给出NaN
for x<0
。
When doing a fit, the parameter values can take any values unless you explicitly restrict the range of values.进行拟合时,参数值可以采用任何值,除非您明确限制值的范围。
Neither your b
variable or your n
variable are bounded at all.你的b
变量或你的n
变量都没有界限。 n
could certainly go below zero during the fit.在拟合过程中, n
肯定可以低于零 go。 If b
goes below -1000, then some values of b+x
will also be below 0. Either one will cause a nan
value in your model and the fit will stop immediately.如果b
低于 -1000,则b+x
的某些值也将低于 0。任何一个都会在您的 model 中产生一个nan
值,并且拟合将立即停止。
That is why the message says "Please check your model function and/or set boundaries on parameters where applicable".这就是消息显示“请检查您的 model function 和/或在适用的参数上设置边界”的原因。 You will need to set bounds to prevent the argument of log()
from being 0 or negative.您将需要设置边界以防止log()
的参数为 0 或负数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.