Python.scipy:Curve_fit一个具有多个输入和输出的函数

时间:2017-06-19 15:08:23

标签: python optimization parameters scipy

我想在一个函数中安装两个参数(Km,kcat)和4个输入和输出到某些数据。 功能是:

def dxdt(x, t, Km, kcat):
    y = np.zeros(4)
    reaction1 = Km * x[2] * x[0]
    reaction2 = kcat * x[3]
    y[0] = - reaction1
    y[1] = reaction2
    y[2] = reaction2 - reaction1
    y[3] = reaction1 - reaction2
return y
例如,数据可以是:

x = [[7.2, 6.1, 5.5, 4.1, 3.3, 2.1, 1.9, 0.2], [0., 1.1, 2.2, 3.5, 
4.5, 6.0, 6.6, 7.3], [10., 9.5, 8., 7.1, 6.8, 5., 5.9, 9.9], [0., 1.2, 
3., 4.5, 5., 6.1, 2.5, 0.4 ]]

有时间跨度:

t = range(9)

并修正起始值:

initial_values = [7.2., 0., 10., 0.]

我定义了一个函数,它计算给定参数集的输出:

def y(timerange, a, b):    
    result = odeint(dxdt, initial_values, timerange, 
                    args=(a, b))
return result.transpose()

现在,我不知道如何正确使用scipy.optimize.curvefit()。 我只看到了一些函数来自R ^ n - >的curve_fit示例。 R但不是来自R ^ n-> R ^ n。 这甚至可能吗?

1 个答案:

答案 0 :(得分:0)

使用可以使用leastsq

import numpy as np
from scipy.optimize import leastsq
from scipy.integrate import odeint
def dxdt(x, t, Km, kcat):
    y = np.zeros(4)
    reaction1 = Km * x[2] * x[0]
    reaction2 = kcat * x[3]
    y[0] = - reaction1
    y[1] = reaction2
    y[2] = reaction2 - reaction1
    y[3] = reaction1 - reaction2
    return y
x = np.array([[7.2, 6.1, 5.5, 4.1, 3.3, 2.1, 1.9, 0.2], [0., 1.1, 2.2, 3.5, 4.5, 6.0, 6.6, 7.3], [10., 9.5, 8., 7.1, 6.8, 5., 5.9, 9.9], [0., 1.2, 3., 4.5, 5., 6.1, 2.5, 0.4 ]])
t = range(8)
initial_values = [7.2, 0., 10., 0.]
def y(timerange, a, b):    
    result = odeint(dxdt, initial_values, timerange, 
                args=(a, b))
    return result.transpose()
def residuals(params):
    a,b=params
    return (y(t,a,b)-x).flatten()
result=leastsq(residuals,[0.1,0.1])
print(result)  
相关问题