Python Scipy 曲线拟合运行时错误:找不到最佳参数:函数调用次数已达到 maxfev = 1000
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15831763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000
提问by Tjitze
I want to make an logharitmic fit. But I keep getting the a runtime error:
我想进行对数拟合。但我不断收到运行时错误:
Optimal parameters not found: Number of calls to function has reached maxfev = 1000
未找到最佳参数:函数调用次数已达到 maxfev = 1000
I use the following script. Can anyone tell me where I go wrong? I use Spyder an am still a beginner.
我使用以下脚本。谁能告诉我我哪里出错了?我使用 Spyder 仍然是初学者。
import math
import matplotlib as mpl
from scipy.optimize import curve_fit
import numpy as np
#data
F1=[735.0,696.0,690.0,683.0,680.0,678.0,679.0,675.0,671.0,669.0,668.0,664.0,664.0]
t1=[1,90000.0,178200.0,421200.0,505800.0,592200.0,768600.0,1036800.0,1371600.0,1630800.0,1715400.0,2345400.0,2409012.0]
F1n=np.array(F1)
t1n=np.array(t1)
plt.plot(t1,F1,'ro',label="original data")
# curvefit
def func(t,a,b):
return a+b*np.log(t)
t=np.linspace(0,3600*24*28,13)
popt, pcov = curve_fit(func, t, F1n, maxfev=1000)
plt.plot(t, func(t, *popt), label="Fitted Curve")
plt.legend(loc='upper left')
plt.show()
回答by StuGrey
After fixing your import statements:
修复导入语句后:
#import matplotlib as mpl
import matplotlib.pyplot as plt
your code produced the following error:
您的代码产生了以下错误:
RuntimeWarning: divide by zero encountered in log
changing:
改变:
#t=np.linspace(0,3600*24*28,13)
t=np.linspace(1,3600*24*28,13)
produced the following output:
产生了以下输出:


回答by unutbu
Your original data is t1and F1. Therefore curve_fitshould be given t1as its second argument, nott.
您的原始数据是t1和F1。因此curve_fit应该t1作为它的第二个参数给出,而不是t。
popt, pcov = curve_fit(func, t1, F1, maxfev=1000)
Now once you obtain fitted parameters, popt, you can evaluate funcat the points in tto obtain a fitted curve:
现在,一旦您获得拟合参数 ,popt您就可以func在 中的点处进行评估t以获得拟合曲线:
t = np.linspace(1, 3600 * 24 * 28, 13)
plt.plot(t, func(t, *popt), label="Fitted Curve")
(I removed zero from t(per StuGrey's answer) to avoid the Warning: divide by zero encountered in log.)
(我从t(根据 StuGrey 的回答)中删除了零以避免Warning: divide by zero encountered in log.)
import matplotlib.pyplot as plt
import scipy.optimize as optimize
import numpy as np
# data
F1 = np.array([
735.0, 696.0, 690.0, 683.0, 680.0, 678.0, 679.0, 675.0, 671.0, 669.0, 668.0,
664.0, 664.0])
t1 = np.array([
1, 90000.0, 178200.0, 421200.0, 505800.0, 592200.0, 768600.0, 1036800.0,
1371600.0, 1630800.0, 1715400.0, 2345400.0, 2409012.0])
plt.plot(t1, F1, 'ro', label="original data")
# curvefit
def func(t, a, b):
return a + b * np.log(t)
popt, pcov = optimize.curve_fit(func, t1, F1, maxfev=1000)
t = np.linspace(1, 3600 * 24 * 28, 13)
plt.plot(t, func(t, *popt), label="Fitted Curve")
plt.legend(loc='upper left')
plt.show()


回答by Hanrong Zheng
Curve_fit()uses iterations to search for optimal parameters. If the number of iterations exceeds the set number of 1000, but the optimal parameters are still not available, then this error will be raised. You can provide some initial guess parameters for curve_fit(), then try again.
Curve_fit()使用迭代来搜索最优参数。如果迭代次数超过设定的 1000 次,但仍然无法获得最优参数,则会引发此错误。您可以为 提供一些初始猜测参数curve_fit(),然后重试。
回答by embulldogs99
Scipy's
西比的
curve_fit()
uses iterations to search for optimal parameters. If the number of iterations exceeds the default number of 800, but the optimal parameters are still not found, then this error will be raised.
使用迭代来搜索最优参数。如果迭代次数超过默认的 800 次,但仍然没有找到最优参数,则会引发此错误。
Optimal parameters not found: Number of calls to function has reached maxfev = 800
You can provide some initial guess parameters for curve_fit(), then try again. Or, you can increase the allowable iterations. Or do both!
您可以为 curve_fit() 提供一些初始猜测参数,然后重试。或者,您可以增加允许的迭代次数。或者两者都做!
Here is an example:
下面是一个例子:
popt, pcov = curve_fit(exponenial_func, x, y, p0=[1,0,1], maxfev=5000)
p0 is the guess
p0 是猜测
maxfev is the max number of iterations
maxfev 是最大迭代次数
You can also try setting bounds which will help the function find the solution. However, you cannot set bounds and a max_nfev at the same time.
您还可以尝试设置边界,这将有助于函数找到解决方案。但是,您不能同时设置边界和 max_nfev。
popt, pcov = curve_fit(exponenial_func, x, y, p0=[1,0,1], bounds=(1,3))
Source1: https://github.com/scipy/scipy/issues/6340
来源 1:https: //github.com/scipy/scipy/issues/6340
Source2: My own testing and finding that the about github is not 100% accurate
Source2:我自己测试发现关于github不是100%准确
Also, other recommendations about not using 0 as an 'x' value are great recommendations. Start your 'x' array with 1 to avoid divide by zero errors.
此外,关于不使用 0 作为“x”值的其他建议也是很好的建议。以 1 开始您的 'x' 数组以避免除以零错误。

