英文:
Scipy optimize curve_fit not responding properly
问题
This code returns a tiny horizontal line at the coordinates of the first data point. What am I doing wrong? I am also getting an OptimizeWarning: Covariance of the parameters could not be estimated error.
英文:
from scipy import optimize
import matplotlib.pyplot as plt
x_data = np.linspace(0,11,12)
y_data = np.array([0.02471114, 0.02057292, 0.01752668, 0.01494543, 0.01273249, 0.0110999 , 0.00946524, 0.00805622, 0.00670716, 0.00558925, 0.00465331, 0.00387775])
def func(x,a,b):
return ((a-1)*(-b*x - (0.024711**(1-a)/(1-a))))**(1/(1-a))
popt,pcov = optimize.curve_fit(func, x_data, y_data, p0=[1.1,0.1])
a_opt, b_opt = popt
x_model = np.linspace(min(x_data), max(y_data), 100)
y_model = func(x_model, a_opt, b_opt)
plt.scatter(x_data, y_data)
plt.plot(x_model, y_model, color='r')
plt.show()
This returns a tiny horizontal line at the coordinates of the first datapoint. What am I doing wrong? I am also getting an OptimizeWarning:
Covariance of the parameters could not be estimated error.
答案1
得分: 0
可能的错误在于 x_model = np.linspace(min(x_data), max(y_data), 100)
,我猜想它应该是 x_model = np.linspace(min(x_data), max(x_data), 100)
。
另外,正如 @9769953 的评论中提到的,你应该使用一个初始猜测值为 [1.1, -0.1]。
有了这些改变,拟合看起来对我来说是正常的。
英文:
Probably the error is in x_model = np.linspace(min(x_data), max(y_data), 100)
which I guess should really be x_model = np.linspace(min(x_data), max(x_data), 100)
.
Also, as noted in the comment by @9769953 you should use an initial guess of [1.1, -0.1].
With these changes, the fit looks ok to me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论