栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么会这样scipy.optimize.curveu fit曲线拟合不符合数据?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么会这样scipy.optimize.curveu fit曲线拟合不符合数据?

数值算法往往更好地工作时,没有饲料非常小(或
(大)数字。
在本例中,图表显示数据的x和y值非常小。如果
如果缩放它们,拟合效果会更好:

xData = np.load('xData.npy')*10**5yData = np.load('yData.npy')*10**5

from __future__ import divisionimport osos.chdir(os.path.expanduser('~/tmp'))import numpy as npimport scipy.optimize as optimizeimport matplotlib.pyplot as pltdef func(x,a,b,c):   return a*np.exp(-b*x)-cxData = np.load('xData.npy')*10**5yData = np.load('yData.npy')*10**5print(xData.min(), xData.max())print(yData.min(), yData.max())trialX = np.linspace(xData[0], xData[-1], 1000)# Fit a polynomial fitted = np.polyfit(xData, yData, 10)[::-1]y = np.zeros(len(trialX))for i in range(len(fitted)):   y += fitted[i]*trialX**i# Fit an exponentialpopt, pcov = optimize.curve_fit(func, xData, yData)print(popt)yEXP = func(trialX, *popt)plt.figure()plt.plot(xData, yData, label='Data', marker='o')plt.plot(trialX, yEXP, 'r-',ls='--', label="Exp Fit")plt.plot(trialX, y, label = '10 Deg Poly')plt.legend()plt.show()

Note that after rescaling

xData
and
yData
, the parameters returned by
curve_fit
must also be rescaled. In this case,
a
,
b
and
c
each must be
divided by 10**5 to obtain fitted parameters for the original data.


One objection you might have to the above is that the scaling has to be chosen
rather “carefully”. (Read: Not every reasonable choice of scale works!)

You can improve the robustness of

curve_fit
by providing a reasonable
initial guess for the parameters. Usually you have some a priori knowledge
about the data which can motivate ballpark / back-of-the envelope type guesses
for reasonable parameter values.

For example, calling

curve_fit
with

guess = (-1, 0.1, 0)popt, pcov = optimize.curve_fit(func, xData, yData, guess)

helps improve the range of scales on which

curve_fit
succeeds in this case.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624743.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号