有没有更高效的方式来处理python中的多个for循环?

时间:2019-11-12 08:49:58

标签: python loops numpy matrix

我有实验数据,可以使用Python拟合数值模型。因此,为了做到这一点,我必须找到两个关键参数的最佳组合。我所做的就是定义两个向量,这些向量包含这些参数的大量值,然后进行两个for循环以找到适合我的数据的最佳组合,如下所示:

## definition of the variable parameters ISC and STA
kISC_vector = np.logspace(5, 7, 100)
kSTA_vector = np.logspace(-8, -13, 1000) * 10**(-6) * N 


data_to_write = np.zeros([len(kISC_vector)*len(kSTA_vector),3])

# for loop over all the value of kISC and kSTA
for ii in range(len(kISC_vector)) :
    for jj in range(len(kSTA_vector)) : 

    ###### solution of the system ######

    S_init = np.array([1,0,0])
    # state population initialization (S0 = 1, S1 = 0, T1 = 0)

    S1_solution = np.zeros((len(t),1)) 
    # 2D array to store the value of S1,T1 after each iteration of the following for loop

    Ip = Ipump[2]


    kISC = kISC_vector[ii]
    kSTA = kSTA_vector[jj]


    # vector of the parameters
    p = [Ip, Sp, kSTA, kISC]

    # solver 
    solution  = odeint(equation, S_init, t, args = (p,))


    # storage of the S1 solution vectors
    S1_solution = solution[:,1]

    # Normalization
    S1_solution /= np.amax( S1_solution )

    # compute the goodness of the fit compared to the experimental data 
    sigma = np.sqrt( np.sum( (PL_3_exp - S1_solution)**2 ) / len(t_exp) )

    #plt.plot(t*10**9, S1_solution, '-')

    # Gather all the relevant data 
    data_to_write[ii*len(kSTA_vector) + jj, :] = [kISC,kSTA/(10**(-6)*N),sigma]

我得到了预期的结果,并且整个过程似乎都很好,但是有没有更优雅的方式来解决这种问题呢?避免多重for循环? 也许用两个参数创建一个网格网格,使用不知道的元组概念。我是这种语言的新手,所以也许你们会知道更多。 谢谢

0 个答案:

没有答案