在数组中存储来自while循环的数据

2024-07-04 15:46:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经写了一个代码,我想用它来减少测量数据。为此,我遍历了30组测量数据。在每次迭代中,我使用fsolve来解一组三个非线性方程。这给了我一个数组,其中包含三个值,然后进一步处理(在下面的示例中,lbdaalpbtadltqN)。我可以打印结果,但需要收集30×6数组中所有30个周期的数据,以便对(即。np.平均值在6个变量中的每一个上)。你知道吗

我在每次迭代结束时都尝试过最明显的函数np.appendnp.vstacknp.concatenates,但这只会给我一个只包含最后一个迭代步骤的1乘6数组,而不是包含所有30个迭代步骤的所需数组。你知道吗


# loading data above 

m1 = data_arr_blkcorr [:,4] / data_arr_blkcorr [:,2]
m2 = data_arr_blkcorr [:,5] / data_arr_blkcorr [:,2] 
m3 = data_arr_blkcorr [:,7] / data_arr_blkcorr [:,2]


N=-1
while (N<29):

    N = N+1 

    T1 = 79.744440299369400 
    T2 = 4.756431967877120 
    T3 = 195.146815878103000 
    T4 = 1.333609171398 
    T5 = 0.540566631391 
    T6 = 1 
    T7 = 1.731261585620 

    T_all = np.array([T4, T5, T6, T7, T1, T2, T3])

    n1 = 0.598169735 
    n2 = 1.509919737 
    n3 = 0.600477235 
    n4 = 0.9364071191658 
    n5 = 0.5815716133216 
    n6 = 1 
    n7 = 1.0455228260642 

    n_all = np.array([n4, n5, n6, n7, n1, n2, n3])


    I1 = 94.905838
    I2 = 96.906018
    I3 = 97.905405
    I4 = 99.907473

    I5 = 91.90681
    I6 = 93.90509
    I7 = 95.90468

# some definition of variables here

    A11 = T1-n1
    A12 = T2-n2
    A13 = T3-n3

    A21 = -n1*P1
    A22 = -n2*P2
    A23 = -n3*P3

    A31 = m1[N] * P1
    A32 = m2[N] * P2
    A33 = m3[N] * P3

    b11 = m1[N] - n1
    b12 = m2[N] - n2
    b13 = m3[N] - n3

# some definition of variables here

    T = np.array ([T1, T2, T3])
    n = np. array([n1, n2, n3])
    m = np.array([m1[N], m2[N], m3[N]])
    P = np.array([P1, P2, P3])


    def F(x):
        return x[0]*T + (1-x[0])*n*np.exp(-x[1]/(1-x[0])*P) - m*np.exp(-x[2]*P) 

    y = fsolve(F, guess)

    lbda = y[0]
    alp = y[1]/(1-y[0])
    bta = y[2]

    dlt = (np.exp(-alp*P2)-1)*1000
    N_all = n_all * np.exp(-alp*P_all)

    q = (1 + (1 - lbda) / lbda * np.sum(N_all) / np.sum(T_all))**(-1)

    print (lbda, alp, bta, dlt, q, N) 

在浏览这些帖子时,我也使用了这句话(根据可可的建议):

data_sum = None
new_data = [lbda, alp, bta, dlt, q, N]
data_sum = np.append([data_sum], new_data) if data_sum is not None else new_data
print(data_sum)

但这会产生一个由30个1×6的数组组成的列表,我基本上无法访问这些数组(即,为所有30个迭代步骤中的单个值计算np.means)。你知道吗

0.0209809690838 0.00142553246898 1.61537217874 -0.0443566490317 0.492710128581 26
(0.020980969083774538, 0.0014255324689812997, 1.6153721787428821, -0.044356649031684903, 0.4927101285811698, 26)
0.0209791772348 0.00272489389093 1.61486845411 -0.0847856651612 0.492691689834 27
(0.020979177234773643, 0.0027248938909269797, 1.6148684541135419, -0.084785665161235535, 0.49269168983354455, 27)
0.0209792771323 0.004884280445 1.61191395635 -0.151970341101 0.49269849879 28
(0.020979277132325381, 0.0048842804449965851, 1.6119139563515672, -0.15197034110059349, 0.4926984987899769, 28)
0.0209799414614 0.00256323393277 1.61366560195 -0.0797557810515 0.492700571038 29
(0.020979941461433328, 0.0025632339327746521, 1.6136656019498359, -0.079755781051460417, 0.49270057103806092, 29)

而且,它会在多次运行中连接结果(即使在关闭Python并重新启动它之后),而且我无法清除这种内存。你知道吗


Tags: datanp数组allarraysumarrn2
2条回答

您应该在while循环范围外声明一个空列表,然后在每次迭代时附加到该列表:

result = []
while(N<29):
   # calculate something
   result.append(your_data)

print(result)   # that will give you all the data that you got from each Iteration

尝试在while循环外创建一个空列表,然后附加数组。你知道吗

solution = []
while n < 29:
    #your code here
    solution.append([lbda, alp, bta, dlt, q, N])

相关问题 更多 >

    热门问题