【问题标题】:Lopping over an array of parameter values遍历参数值数组
【发布时间】:2021-04-08 01:59:26
【问题描述】:

我对 Python 编码还很陌生,因此我们将不胜感激,如果需要澄清,请告诉我。

以下是我的工作代码。它是一个 SIR 模型,包含一个由 7 个一阶微分方程组成的系统,具有一些参数值和初始条件。

import scipy.integrate
import matplotlib.pyplot as plt
import numpy as np

def ode(t, y):
    sigma1 = 1/10
    sigma2 = 1/4
    alpha = 1/10.4
    gamma = 1/5
    f = 0.18122
    h = 0.24045
    dh = 0.06218
    d = 0.02591
    beta = 0.4
    N = 500000

    return np.array([-(beta*(y[2]+y[3])/N)*y[0],
                     (beta*(y[2]+y[3])/N)*y[0]-gamma*y[1],
                     f*gamma*y[1]-sigma1*y[2],
                     (1-f)*gamma*y[1]-sigma2*y[3],
                     h*sigma1*y[2]-alpha*y[4],
                     (1-h-d)*sigma1*y[2]+sigma2*y[3]+(1-dh)*alpha*y[4], 
                     d*sigma1*y[2]+dh*alpha*y[4]]) 
t0 = 0 
t_bound = 100 
y0 = np.array([480000,0,10000,10000,0,0,0])
sol = scipy.integrate.RK45(ode, t0, y0, t_bound)
t = []
y = []
while sol.status == "running":
    t.append(sol.t)
    y.append(sol.y)
    sol.step()

plt.plot(np.array(t), np.array(y))
plt.legend(("susceptible", "exposed","infectious_s", "infectious_a", "hospitalized", "recovered", "deaths"))
plt.xlabel("time")
plt.ylabel("cases")
plt.show()

我想修改我的代码,以便我可以为我的所有参数设置一组值,而不是一个数值。举个例子,像这样:

    h = ([0.182,0.055,0.055,0.055,0.068,0.068,0.139,0.139,0.139,0.139,0.251,0.251,0.251,0.512,0.512,0.512,0.617])
    dh = ([0.002, 0, 0, 0, 0.002, 0.002, 0.009, 0.009, 0.009, 0.009, 0.036, 0.036, 0.036, 0.149, 0.149, 0.149, 0.328])
    d = ([0.001, 0, 0, 0, 0.001, 0.001, 0.004, 0.004, 0.004, 0.004, 0.014, 0.014, 0.014, 0.059, 0.059, 0.059, 0.129])

我打算根据参数对计算进行矢量化处理,因此我得到的不是每个时间步的单个解决方案,而是一组解决方案。话虽如此,我的问题是如何正确循环参数值以获得这样的解决方案?

【问题讨论】:

    标签: python arrays vectorization ode


    【解决方案1】:

    尝试使用一个新数组来存储每个循环结果,并使用一个 for 循环来获取每个参数。

    solutions_array = []
    
    for index in range(len(h)):
        actual_h = h[index]
        actual_dh = dh[index]
        actual_d = d[index]
        # The rest of your calculations
        # End of calculation in variable result
        solutions_array.append(result)
    

    在每个循环中,actual_x 变量将包含所需的参数。

    还需要从数组中删除(),因为元组中有一个列表(数组),这是多余的。你应该有:

    h = [0.182,0.055,0.055,0.055,0.068,0.068,0.139,0.139,0.139,0.139,0.251,0.251,0.251,0.512,0.512,0.512,0.617]
    dh = [0.002, 0, 0, 0, 0.002, 0.002, 0.009, 0.009, 0.009, 0.009, 0.036, 0.036, 0.036, 0.149, 0.149, 0.149, 0.328]
    d = [0.001, 0, 0, 0, 0.001, 0.001, 0.004, 0.004, 0.004, 0.004, 0.014, 0.014, 0.014, 0.059, 0.059, 0.059, 0.129]
    

    更优雅的方法是使用一个包含每个循环的每个参数的简单数组,例如:

    parameters = [[0.182, 0.02, 0.001],[0.055, 0, 0],[0.055, 0, 0],[0.068, 0.002, 0]] #[h, dh, d]
    
    solutions_array = []
    for h, dh, d in parameters:
        # The rest of your calculations
        # End of calculation in variable result
        solutions_array.append(result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2020-12-18
      相关资源
      最近更新 更多