当其中一个拟合参数更改 xdata 输入数组值时,我可以使用 scipy.curve fit in python 吗?

Can I use scipy.curve fit in python when one of the fitted parameters changes the xdata input array values?

这是我第一次发布问题,我会尽量把问题说清楚,但请随时提问。

我正在尝试使用如下 scipy.curve_fit 方法将模型拟合到曲线:

import numpy as np
import matplotlib.pyplot as pyplot
import scipy
from scipy.optimize import curve_fit




def func2(x,EM):
    return (((4.0*EM*(np.sqrt(8*10**-9)))/(3.0*(1.0-(0.5**2))*8*10**-9))*(((((x))*1*10**-9)**((3.0/2.0)))))


ydata=[-0.003428768, -0.009050058, -0.0037997673999999996, -0.0003833233, -0.007557649, -0.0034860994, -0.0009856887, -0.0017508664, -0.00036931394999999996, 
       -0.0040713947, -0.005737315000000001, 0.0005120568, -0.007336486, -0.00719302, -0.0039941817, -0.0029785274, -0.0013044578, -0.008190335, -0.00833507,
       -0.0074282060000000006, -0.009629990000000001, -0.009425125, -0.008662485999999999, -0.0019445216, -0.008331748, -0.009513038, -0.0047609017, -0.004364422,
       -0.010325097, -0.0036570733, -0.0060091914, -0.005655772, -0.0045517069999999995, -0.00066998035, 0.006374902, 0.006445733, 0.0019101816,
       0.010262737999999999, 0.011139007, 0.018161469, 0.016963122, 0.022915895, 0.027177791, 0.028707139, 0.040105638, 0.044088004, 0.041657403,
       0.052325636999999994, 0.062399405, 0.07020844, 0.076979915, 0.08888523, 0.099634745, 0.10961602, 0.12188646, 0.13677225, 0.15639512, 0.16833586,
       0.18849944000000002, 0.21515548, 0.23989769000000002, 0.26319308, 0.29388397, 0.321042, 0.35637776, 0.38564656999999997, 0.4185209, 0.44986692,
       0.48931552999999994, 0.52583893, 0.5626885, 0.6051665, 0.6461075, 0.69644346, 0.7447817, 0.7931281, 0.8381386000000001, 0.8883482, 0.9395609999999999,
       0.9853629, 1.0377034, 1.0889026, 1.1334094]


xdata=[34.51388, 33.963736999999995, 
       33.510695, 33.04127, 32.477253, 32.013624, 31.536019999999997, 31.02925, 30.541649999999997, 
       30.008646, 29.493828, 29.049707, 28.479668, 27.980956, 27.509590000000003, 27.018721, 26.533737, 25.972296, 
       25.471065, 24.979228000000003, 24.459624, 23.961517, 23.46839, 23.028454, 22.471411, 21.960924, 21.503428000000003, 
       21.007033, 20.453855, 20.013475, 19.492528, 18.995746999999998, 18.505670000000002, 18.040403, 17.603387, 17.104082, 
       16.563634, 16.138298000000002, 15.646187, 15.20897, 14.69833, 14.25156, 13.789688, 13.303409, 12.905278, 12.440909, 11.919262, 
       11.514609, 11.104646, 10.674512, 10.235055, 9.84145, 9.437523, 9.026733, 8.63639, 8.2694065, 7.944733, 7.551445, 7.231599999999999, 
       6.9697434, 6.690793299999999, 6.3989780000000005, 6.173159, 5.9157856, 5.731453, 5.4929328, 5.2866156, 5.066648000000001, 4.9190496, 
       4.745381399999999, 4.574569599999999, 4.4540283, 4.3197597000000005, 4.2694026, 4.2012034, 4.133134, 4.035212, 3.9837262, 3.9412007, 3.8503475999999996, 
       3.8178950000000005, 3.7753053999999997, 3.6728842]


dstart=20.0 

xdata=np.array(xdata[::-1])
xdata=xdata-dstart
xdata=list(xdata)

xdata1=[]
ydata1=[]
for i in range(len(xdata)):
    if xdata[i]>0:
        xdata1.append(xdata[i])
        ydata1.append(ydata[i])

xdata=np.array(xdata1)
ydata=np.array(ydata1)

popt, pcov = curve_fit(func2, xdata, ydata)
a=popt[0]

print "E=", popt[0]/10**6


t=func2(xdata,a)

ax=pyplot.figure().add_subplot(1,1,1)
ax.plot(xdata,t, color="blue",mew=2.0,label="Hertz Fit")
ax.plot(xdata,ydata,ls="",marker="x",color="red",mew=2.0,label="Data")
ax.legend(loc=2)
pyplot.show()

"dstart" 值基本上截掉了我不想拟合的代码的下半部分,因为它是负数并且模型不喜欢负数。目前我必须在 运行 代码之前手动设置 "dstart" 然后我看到最终结果。

我开始在 Excel 中使用 Solver 进行拟合,通过嵌套通过 [=28 调整 xdata 的代码同时改变 "EM" 变量和 "dstart" 变量=] 并将负值截断到正在拟合的函数中。

基本上我想要的是:

import numpy as np
import matplotlib.pyplot as pyplot
import scipy
from scipy.optimize import curve_fit




def func2(x,EM,dstart): 

    xdata=np.array(x[::-1])
    xdata=dstart-xdata
    xdata=list(xdata)

    xdata1=[]
    for i in range(len(xdata)):
        if xdata[i]>0:
            xdata1.append(xdata[i])

    global xdata2
    xdata2=np.array(xdata1)






    return (((4.0*EM*(np.sqrt(8*10**-9)))/(3.0*(1.0-(0.5**2))*8*10**-9))*(((((xdata2))*1*10**-9)**((3.0/2.0)))))


ydata=[-0.003428768, -0.009050058, -0.0037997673999999996, -0.0003833233, -0.007557649, -0.0034860994, -0.0009856887, -0.0017508664, -0.00036931394999999996, 
       -0.0040713947, -0.005737315000000001, 0.0005120568, -0.007336486, -0.00719302, -0.0039941817, -0.0029785274, -0.0013044578, -0.008190335, -0.00833507,
       -0.0074282060000000006, -0.009629990000000001, -0.009425125, -0.008662485999999999, -0.0019445216, -0.008331748, -0.009513038, -0.0047609017, -0.004364422,
       -0.010325097, -0.0036570733, -0.0060091914, -0.005655772, -0.0045517069999999995, -0.00066998035, 0.006374902, 0.006445733, 0.0019101816,
       0.010262737999999999, 0.011139007, 0.018161469, 0.016963122, 0.022915895, 0.027177791, 0.028707139, 0.040105638, 0.044088004, 0.041657403,
       0.052325636999999994, 0.062399405, 0.07020844, 0.076979915, 0.08888523, 0.099634745, 0.10961602, 0.12188646, 0.13677225, 0.15639512, 0.16833586,
       0.18849944000000002, 0.21515548, 0.23989769000000002, 0.26319308, 0.29388397, 0.321042, 0.35637776, 0.38564656999999997, 0.4185209, 0.44986692,
       0.48931552999999994, 0.52583893, 0.5626885, 0.6051665, 0.6461075, 0.69644346, 0.7447817, 0.7931281, 0.8381386000000001, 0.8883482, 0.9395609999999999,
       0.9853629, 1.0377034, 1.0889026, 1.1334094]


xdata=[34.51388, 33.963736999999995, 
       33.510695, 33.04127, 32.477253, 32.013624, 31.536019999999997, 31.02925, 30.541649999999997, 
       30.008646, 29.493828, 29.049707, 28.479668, 27.980956, 27.509590000000003, 27.018721, 26.533737, 25.972296, 
       25.471065, 24.979228000000003, 24.459624, 23.961517, 23.46839, 23.028454, 22.471411, 21.960924, 21.503428000000003, 
       21.007033, 20.453855, 20.013475, 19.492528, 18.995746999999998, 18.505670000000002, 18.040403, 17.603387, 17.104082, 
       16.563634, 16.138298000000002, 15.646187, 15.20897, 14.69833, 14.25156, 13.789688, 13.303409, 12.905278, 12.440909, 11.919262, 
       11.514609, 11.104646, 10.674512, 10.235055, 9.84145, 9.437523, 9.026733, 8.63639, 8.2694065, 7.944733, 7.551445, 7.231599999999999, 
       6.9697434, 6.690793299999999, 6.3989780000000005, 6.173159, 5.9157856, 5.731453, 5.4929328, 5.2866156, 5.066648000000001, 4.9190496, 
       4.745381399999999, 4.574569599999999, 4.4540283, 4.3197597000000005, 4.2694026, 4.2012034, 4.133134, 4.035212, 3.9837262, 3.9412007, 3.8503475999999996, 
       3.8178950000000005, 3.7753053999999997, 3.6728842]

xdata2=list(xdata2)
ydata1=[]
for i in range(len(xdata2)):
    if xdata2[i]>0:
        ydata1.append(ydata[i])




popt, pcov = curve_fit(func2, xdata, ydata)

但这不起作用,因为我收到值错误 "ValueError: operands could not be broadcast together with shapes (28,) (30,)"。我认为我需要的是 curve_fit 引入扩展数据,根据第一个猜测的 "dstart" 进行调整,猜测 EM 并检查是否适合并最小化错误,尝试使用新的 "dstart" 进行调整xdata,猜测 EM 并检查拟合和最小化错误,等等。因为我对 Python 还是相当陌生,所以我肯定不适合曲线拟合,如果我没有可能有数千条曲线到 [=41=,我只会使用 Excel ].

如有任何帮助,我们将不胜感激!

我将其分为两部分:概念和编码相关

概念:

让我们从重新表述您的问题开始。就目前而言,答案是:是的,显然。在目标函数中简单地吸收parameter-dependent变化x。但这并不能解决你的问题。您真正感兴趣的是如何处理您的函数无法处理某些 x 的参数。没有 one-size-fits-all。

您可以选择将此类参数视为不可接受的,在这种情况下您将不得不求助于约束优化。 scipy 中有一些求解器可以做到这一点。

您可以选择在拟合前去除数据集中的难点

您可以引入软约束和 penalise 错误值,而不是完全排除它们。

编程风格:

for 数值程序中的循环。这个网站上有无数的帖子,所以我只举一个例子:

xdata2=list(xdata2)
ydata1=[]
for i in range(len(xdata2)):
    if xdata2[i]>0:
        ydata1.append(ydata[i])

可以写在一行中,执行速度会快得多,return 一个 array 而不是 list:

ydata1 = ydata[xdata2 > 0]

如果您想学习此技术,请查看 numpy tutorial/docs 或搜索此站点 "vectorization"。

除此之外,没有任何抱怨。

为什么你的第二个程序不起作用。

您正在筛选 xy,因此它们的形状应该相同。但是你继续使用旧副本而不是新的 y 而你确实使用了新的 x。这就是形状不匹配的原因

顺便说一句。您设置它的方式(在 func2 内修改 x)或多或少地实施了我之前提到的 absorb 策略。只是,由于您无权访问 y,因此您无法更改 x.

的形状