英文:
Curve_fit RuntimeWarning message in Scipy python
问题
我试图使用curve_fit拟合我的数据。然而,我收到了这个警告消息。
RuntimeWarning: 在对数中遇到无效值
我查找过,这意味着在对数中存在负值。然而,我的数据中并没有负值。有人可以帮忙吗?
这是我的代码:
from scipy.optimize import curve_fit
import numpy as np
def fit(x,a,b):
return a*np.log(b*x+1)
q = [0., 4.698, 9.396, 13.148, 18.792, 22.416, 26.296, 30.176, 34.056]
r = [0., 1.50755518, 3.53050467, 6.5067685, 9.8676480, 12.8481805, 14.6121058, 15.5656062, 16.69052756]
fit_log_param, _ = curve_fit(fit, xdata=q, ydata=r)
英文:
I am trying to fit my data with curve_fit. However, I get this warning message.
> RuntimeWarning: invalid value encountered in log
I searched that this means there's negative value in log. However, I don't have negative values in my data. Can anyone help?
Here is my code:
from scipy.optimize import curve_fit
import numpy as np
def fit(x,a,b):
return a*np.log(b*x+1)
q = [0., 4.698, 9.396, 13.148, 18.792, 22.416, 26.296, 30.176, 34.056]
r = [0., 1.50755518, 3.53050467, 6.5067685, 9.8676480, 12.8481805, 14.6121058, 15.5656062, 16.69052756]
fit_log_param, _ = curve_fit(fit, xdata=q, ydata=r)
答案1
得分: 0
这是正确的。
发生这种情况的原因是因为curve_fit尝试改变各种参数,并计算chi^2或其他指标以尝试找到一个含义。改变这些参数可能会引发这类警告。例如,当b变为负值时会发生这种情况:b*x+1
可能会成为对数函数的输入的负值。
最终的指标通常会是无穷大或NaN,并被认为比之前的任何指标都要糟糕,因此这些参数值会很快被丢弃,认为它们是“不好的”。它们不应该妨碍拟合,但会使其变慢(即使只是轻微地)。
如果你使用一些接近你预期的起始参数来启动curve_fit
,通常会有以下好处:
- 更快的结果(更少的迭代次数)
- 没有警告消息
- 有一个结果(一些糟糕的起始参数可能会失败,找不到一个良好的最优解)
因此,提供一些良好的起始参数可能是每次使用curve_fit
时的良好做法,无论你要拟合哪种类型的函数。
英文:
> this means there's negative value in log
That is correct.
The reason that happens is because curve_fit tries altering various parameters, and calculates chi^2 or some other metric to try and find a meaning. Varying these parameters may cause these kind of warnings. This could happen, for example, when b gets negative: b*x+1
may become a negative value as inupt for the log.
The resulting metric will often be infinite or NaN and be considered worse than any metric before, so these parameter values will quickly be discarded as being "not good". They shouldn't hamper the fitting, but will slow it down (if only slightly).
If you start curve_fit
with some starting parameters close to what you expect, you often have
- a quicker result (less iterations)
- no warning messages
- a result at all (some bad starting parameters may fail to find a good optimum)
So provide some good starting parameters. That is probably good practice every time you use curve_fit
, with any type of function you're trying to fit.
答案2
得分: 0
def fit(x, a, b):
return a * np.log(b * x + 1)
q = [0., 4.698, 9.396, 13.148, 18.792, 22.416, 26.296, 30.176, 34.056]
r = [0., 1.50755518, 3.53050467, 6.5067685, 9.8676480, 12.8481805, 14.6121058, 15.5656062, 16.69052756]
fit_log_param, _ = curve_fit(fit, xdata=q, ydata=r, bounds=[(-2, 0), (500, 100)])
英文:
def fit(x,a,b):
return a*np.log(b*x+1)
q = [0., 4.698, 9.396, 13.148, 18.792, 22.416, 26.296, 30.176, 34.056]
r = [0., 1.50755518, 3.53050467, 6.5067685, 9.8676480, 12.8481805, 14.6121058, 15.5656062, 16.69052756]
fit_log_param, _ = curve_fit(fit, xdata=q, ydata=r, bounds=[(-2, 0), (500, 100)])
The log means in the search process of curve_fit, b*x+1 may be negative, so you can limit the bound of the parameters with the param bounds to avoid this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论