返回介绍

Curve fitting

发布于 2025-02-25 23:43:54 字数 1523 浏览 0 评论 0 收藏 0

Sometimes, we simply want to use non-linear least squares to fit a function to data, perhaps to estimate paramters for a mechanistic or phenomenological model. The curve_fit function uses the quasi-Newton Levenberg-Marquadt aloorithm to perform such fits. Behind the scnees, curve_fit is just a wrapper around the leastsq function that we have already seen in a more conveneint format.

from scipy.optimize import curve_fit
def logistic4(x, a, b, c, d):
    """The four paramter logistic function is often used to fit dose-response relationships."""
    return ((a-d)/(1.0+((x/c)**b))) + d
nobs = 24
xdata = np.linspace(0.5, 3.5, nobs)
ptrue = [10, 3, 1.5, 12]
ydata = logistic4(xdata, *ptrue) + 0.5*np.random.random(nobs)
popt, pcov = curve_fit(logistic4, xdata, ydata)
perr = yerr=np.sqrt(np.diag(pcov))
print 'Param\tTrue\tEstim (+/- 1 SD)'
for p, pt, po, pe  in zip('abcd', ptrue, popt, perr):
    print '%s\t%5.2f\t%5.2f (+/-%5.2f)' % (p, pt, po, pe)
Param       True    Estim (+/- 1 SD)
a   10.00   10.26 (+/- 0.15)
b    3.00    3.06 (+/- 0.76)
c    1.50    1.62 (+/- 0.11)
d   12.00   12.41 (+/- 0.20)
x = np.linspace(0, 4, 100)
y = logistic4(x, *popt)
plt.plot(xdata, ydata, 'o')
plt.plot(x, y);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文