【发布时间】:2023-03-28 15:01:01
【问题描述】:
我有一个关于使用scipy.optimize.curve_fit 进行全局拟合的简短问题。据我了解,在局部拟合和全局拟合之间设置脚本的唯一区别是连接函数的区别。以下面的脚本为例:
input_data = [protein, ligand]
titration_data=input('Load titration data')
def fun(_, kd):
a = protein
b = protein + ligand
c = ligand
return np.array((b + kd - np.sqrt(((b + kd)**2) - 4*a*c))/(2*a))
kD=[]
for values in titration_data:
intensity=[values]
intensity_array=np.array(intensity)
x = ligand
y = intensity_array.flatten()
popt, pcov = curve_fit(fun, x, y)
输入数据是一个 6x2 矩阵,滴定数据也是一个 8x6 矩阵。每一行滴定数据将分别拟合到模型中,并获得一个 kd 值。这是局部拟合,现在我想将其更改为全局拟合。根据我对全局拟合的理解,我尝试了以下脚本:
input_data = [protein, ligand]
titration_data=input('Load titration data')
glob=[]
for values in titration_data:
def fun(_, kd):
a = protein
b = protein + ligand
c = ligand
return np.array((b + kd - np.sqrt(((b + kd)**2) - 4*a*c))/(2*a))
print (fun)
glob.append(fun)
def glob_fun(_,kd):
return np.array(glob).flatten()
x = ligand
y = titration_data
popt, pcov = curve_fit(glob_fun, x, y)
根据我的理解,这应该给我一个奇异的 kd 输出,同时拟合所有数据。但是,我在尝试实现此操作时遇到了一条错误消息:
popt, pcov = curve_fit(glob_fun, x, y)
return func(xdata, *params) - ydata
TypeError: unsupported operand type(s) for -: 'function' and 'float'
这里的问题是 glob_fun 实际上是一个函数数组(据我了解,它应该是全局拟合)。但是,似乎不是使用该函数的输出(基于它为 kD 选择的任何内容),而是将其最小化为 ydata,而是使用数组本身的函数之一。因此,您不能减去函数的错误(或者至少,这是我对错误的理解)。
编辑: 我已经添加了数据,因此错误和功能是可重现的。
import numpy as np
from scipy.optimize import curve_fit
concentration= np.array([[0.6 , 0.59642147, 0.5859375 , 0.56603774, 0.53003534,0.41899441],
[0.06 , 0.11928429, 0.29296875, 0.62264151, 1.21908127,3.05865922]])
protein = concentration[0,:]
ligand = concentration[1,:]
input_data = [protein, ligand]
titration_data=np.array([[0, 0, 0.29888413, 0.45540198, 0.72436899,1],
[0,0,0.11930228, 0.35815982, 0.59396978, 1],
[0,0,0.30214337, 0.46685577, 0.79007708, 1],
[0,0,0.27204954, 0.56702549, 0.84013344, 1],
[0,0,0.266836, 0.43993175, 0.74044123, 1],
[0,0,0.28179148, 0.42406587, 0.77048624, 1],
[0,0,0.2281092, 0.50336244, 0.79089151, 0.87029517],
[0,0,0.18317694, 0.55478412, 0.78448465, 1]]).flatten()
glob=[]
for values in titration_data:
def fun(_, kd):
a = protein
b = protein + ligand
c = ligand
return np.array((b + kd - np.sqrt(((b + kd)**2) - 4*a*c))/(2*a))
print (fun)
glob.append(fun)
def glob_fun(_,kd):
return np.array(glob).flatten()
x = ligand
y = titration_data
popt, pcov = curve_fit(glob_fun, x, y)
【问题讨论】:
-
我已经使用 curve_fit 将不同的数据集同时拟合到具有共享参数的多个单独的方程中。这个问题看起来和我当时做的有点相似。如果这听起来正确,那么这里的共享参数是 a、b、c 还是 kd?
-
a、b 和 c 是自变量(它们在所有数据集中都相同),kd 是要解决的问题(即我应该得到的唯一输出是 kd)。
-
你能把这个reproducible example 用你用来产生那个错误的实际数据吗?
-
我以适当的格式输入数据,我发布的脚本将提供相同的错误。
-
我通过扁平化滴定数据解决了上述错误。因此,我没有尝试将输出 fun 转换为 2D,而是将 ydata 转换为 1D。话虽如此,我现在遇到了一个新错误。所以我会保留这个问题(因为它仍然是关于全局拟合的),但将其更改为新的错误)。
标签: python numpy scipy curve-fitting