使用Scipy的Logistic生长曲线不太正确

lnxxn5zx  于 2022-11-23  发布在  其他
关注(0)|答案(3)|浏览(111)

我正在尝试使用Python的Scipy包将一个简单的逻辑增长模型拟合到虚拟数据中。代码如下所示,同时显示了我得到的输出。正确的输出如下所示。我不太清楚这里出了什么问题。

import scipy.optimize as optim
from scipy.integrate import odeint
import numpy as np
import pandas as pd

N0 = 0.37
parsic = [.25, 12.9]

df_yeast = pd.DataFrame({'cd': [9.6, 18.3, 29., 47.2, 71.1, 119.1, 174.6, 257.3, 350.7, 441., 513.3, 559.7, 594.8, 629.4, 640.8, 651.1, 655.9, 659.6], 'td': np.arange(18)})

def logistic_de(t, N, r, K):
    return r*N*(1 - N/K)

def logistic_solution(t, r, K):
    return odeint(logistic_de, N0, t, (r, K), tfirst=True).ravel()

params, _ = optim.curve_fit(logistic_solution, df_yeast['td'], df_yeast['cd'], p0=parsic)

N1 = odeint(logistic_de, N0, np.linspace(0, 20, 10000), (params[0], params[1]), tfirst=True)

plt.plot(np.linspace(0, 20, 10000), N1)
plt.scatter(df_yeast['td'], df_yeast['cd'])
plt.ylabel('num yeast')
plt.xlabel('time')

我的输出:

正确输出:

ne5o7dgx

ne5o7dgx1#

这是他们暗示的编辑,也许这会帮助你理解:

# include N0 as an argument
def logistic_solution(t, N0, r, K):
    return odeint(logistic_de, N0, t, (r, K), tfirst=True).ravel()

# N0 thus included as parameter to fit
params, _ = optim.curve_fit(logistic_solution, df_yeast['td'], df_yeast['cd'], 
                            p0=[N0, *parsic])

# N1 integral factors in the fitted N0 parameter
# (not the same as the global variable named N0,
# should change global variable to something like N0_guess)
N1 = odeint(logistic_de, params[0], np.linspace(0, 20, 10000), 
            tuple(params[1:]), tfirst=True)
beq87vna

beq87vna2#

您的优化不允许更改N0,这与列表中的实际 t=0值有很大不同。

t40tm48m

t40tm48m3#

为什么不尝试直接拟合增长曲线

def my_logistic(t, a, b, c):
    return c / (1 + a * np.exp(-b*t))

params, _ = optim.curve_fit(my_logistic, 
                            df_yeast['td'], df_yeast['cd'],
                            p0=np.random.exponential(size=3),
                            bounds=(0,[100000., 3., 700]))
N1 = my_logistic(np.linspace(0, 20, 10000),*params)

plt.plot(np.linspace(0, 20, 10000), N1)
plt.scatter(df_yeast['td'], df_yeast['cd'])
plt.ylabel('num yeast')
plt.xlabel('time')

得到下面的曲线:

和参数:

[7.18068070e+01 5.47614074e-01 6.62655252e+02]

相关问题