python - 多参数模型的拟合参数不是按新月顺序计算的
问题描述
我为这个有点混乱的标题道歉。
我正在尝试将一组特定的数据(DATA FILE HERE)拟合到由三个 sigmoidal 函数组成的模型,如下所述。
def sigmoid(p,x):
x01,c1,k1,x02,c2,k2,x03,c3,k3,y0=p
y = (c1 / (1 + np.exp(-k1*(x-x01)))) + (c2 / (1 + np.exp(-k2*(x-x02))))+(c3 / (1 + np.exp(-k3*(x-x03)))) + y0
return y
#model for curve with 3 sigmoidal transitions, where:
# y0 = y initial value
# x0, x02, x03 = the x-value of the sigmoid's midpoint
# c1, c2, c3 = the curve's maximum value
# k1, k2, k3= the logistic growth rate or steepness of the curve
当我运行完整的脚本(在下面提供)时,我得到以下示例:
可以看出,c、x0 和 k 的值不是新月顺序(即:c2 的值小于 c3,因此 c3 的值小于 c1)。谁能解释我为什么会发生这种情况以及我该如何解决这个问题?我确实需要参数相对于 x (c1>c2>c3) 处于新月顺序,因为我稍后会将它们存储在变量中以用作统计实体。
在某些情况下,我还会得到 c、k 和 x0 的负值,如下图所示
有没有办法解决这个问题,以便始终获得拟合参数的正值?还是我没有正确安装?
这是我正在运行的代码示例:
import numpy as np
import pandas as pd
import scipy.optimize
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def sigmoid(p,x):
x01,c1,k1,x02,c2,k2,x03,c3,k3,y0=p
y = (c1 / (1 + np.exp(-k1*(x-x01)))) + (c2 / (1 + np.exp(-k2*(x-x02))))+(c3 / (1 + np.exp(-k3*(x-x03)))) + y0
return y
#model for curve with 3 sigmoidal transitions, where:
# y0 = y intitial value,
# x0, x02, x03 = the x-value of the sigmoid's midpoint,
# c1, c2, c3 = the curve's maximum value
# k1, k2, k3= the logistic growth rate or steepness of the curve
def residuals(p,x,y):
return y - sigmoid(p,x)
url=r'/Users/user/Downloads/RawDataEclipse (1).xlsx'
for i in range(12):
#data raw
df = pd.read_excel(url, sheet_name=i,sep='\s*,\s*') # we use index_col=0 to indicate that first row in the dataset is the index column
#df.rename(columns={ df.columns[1]: "Time_1", df.columns[3]: "Time_2"}) #rename columns which have the same name
x1=np.array(df['Time'], dtype=float)
y1=np.array(df['Intensity330'], dtype=float)
x2=np.array(df['Time.1'], dtype=float)
y2=np.array(df['Intensity330'], dtype=float)
x3=np.array(df['Time.2'], dtype=float)
y3=np.array(df['Intensity490\'Time'], dtype=float)
p_guess=(0,0,0,0,0,0,0,0,0,0)
#fits
p1, cov1, infodict1, mesg1, ier1 = scipy.optimize.leastsq(residuals,p_guess,args=(x1,y1),full_output=1)
p2, cov2, infodict2, mesg2, ier2 = scipy.optimize.leastsq(residuals,p_guess,args=(x2,y2),full_output=1)
p3, cov3, infodict3, mesg3, ier2 = scipy.optimize.leastsq(residuals,p_guess,args=(x3,y3),full_output=1)
x011,c11,k11,x021,c21,k21,x031,c31,k31,y01=p1
x012,c12,k12,x022,c22,k22,x032,c32,k32,y02=p2
x013,c13,k13,x023,c23,k23,x033,c33,k33,y03=p3
print('''\
DATA FIT 330 a.u.:
x0 step 1= {x011}
x0 step 2= {x021}
x0 step 3= {x031}
y0= {y01}
c step 1= {c11}
c step 2= {c21}
c step 3= {c31}
k step 1 = {k11}
k step 2 = {k21}
k step 3 = {k31}
'''.format(x011=x011,c11=c11,k11=k11,x021=x021,c21=c21,k21=k21,x031=x031,c31=c31,k31=k31,y01=y01))
print('''\
DATA FIT 350 a.u.:
x0 step 1= {x012}
x0 step 2= {x022}
x0 step 3= {x032}
y0= {y02}
c step 1= {c12}
c step 2= {c22}
c step 3= {c32}
k step 1 = {k12}
k step 2 = {k22}
k step 3 = {k32}
'''.format(x012=x012,c12=c12,k12=k12,x022=x022,c22=c22,k22=k22,x032=x032,c32=c32,k32=k32,y02=y02))
print('''\
DATA FIT 490 a.u.:
x0 step 1= {x013}
x0 step 2= {x023}
x0 step 3= {x033}
y0= {y03}
c step 1= {c13}
c step 2= {c23}
c step 3= {c33}
k step 1 = {k13}
k step 2 = {k23}
k step 3 = {k33}
'''.format(x013=x013,c13=c13,k13=k13,x023=x023,c23=c23,k23=k23,x033=x033,c33=c33,k33=k33,y03=y03))
pxp1=sigmoid(p1,x1)
pxp2=sigmoid(p2,x2)
pxp3=sigmoid(p3,x3)
#Plot the results
plt.plot(x1, y1, '.', label='data i330')
plt.plot(x1, pxp1, label='fit i330')
plt.plot(x2, y2, 'v', label='data i350')
plt.plot(x2, pxp2, label='fit i350')
plt.plot(x3, y3, '^', label='data i490')
plt.plot(x3, pxp3, label='fit i490')
plt.xlabel('Time (min)')
plt.ylabel('Intrinsic Fluorescence (a.u.)')
plt.grid(True)
plt.legend()
plt.show()
解决方案
推荐阅读
- angular - 如何在 Angular 7 中显示任何系统格式的日期格式?
- cmake - cmake 的 OpenMPI 基目录的位置
- ruby-on-rails - 阶段中的 NoMethodError#edit
- javascript - 当用户单击推送通知时打开 TWA 应用程序而不是 Web 应用程序
- c++ - 函数模板返回类型
- c# - 如何在 postsharp 中使用 MultipleInheritance
- raku - Raku Native 调用 gtk-clipboard-request-text?
- java - 传递多个文件时,Swing 窗口卡住
- amazon-web-services - AWS:我们可以考虑为单个产品提供“多个 EC2 实例和 RDS”作为水平扩展吗?
- sql - 如何有条件地禁用 SQL 中的联接?