python - 使用scipy的Logistic增长曲线不太正确

标签 python scipy differential-equations

我正在尝试使用 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')

我的输出: enter image description here

正确输出: enter image description here

最佳答案

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

# 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)

关于python - 使用scipy的Logistic增长曲线不太正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69292456/

相关文章:

python - 如何在 R 中求解二阶微分方程?

c - 数值微分方程求解器算法意外出现段错误

Python Speech_recognition无法读取wav文件

python - 当 'a' 和 'b' 离平均值太远时,Python 和 R 中的法线被截断

python - 为 Windows 7 安装哪个 scipy wheel?

python - 在Python中实现欧拉方法来求解ODE

python - 如何将具有无序索引的 Pandas 数据框中的一行移动到第一行?

python - 用keras Grid Search隐藏层数

python - 有没有办法处理异常但只能处理来自特定库的异常?

machine-learning - 验证聚类算法的输出