gpt4 book ai didi

python - 将 'while' 循环中的数据存储在数组中

转载 作者:行者123 更新时间:2023-12-01 07:28:08 25 4
gpt4 key购买 nike

我编写了一个代码,我想用它来减少测量数据。为此,我迭代了 30 组测量数据。在每次迭代中,我使用 fsolve 来求解一组三个非线性方程。这给我一个包含三个值的数组,然后进一步处理这些值(在下面的示例中 lbdaalpbtadltqN)。我可以打印结果,但需要收集 30 x 6 数组中所有 30 个周期的数据来进行一些统计(即 6 个变量中每个变量的 np.mean)。

我已经尝试过对我来说最明显的函数np.appendnp.vstacknp.concatenates在每次迭代结束时,但这只给我一个 1 x 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)

浏览帖子时我也使用过这个(在 Koke Cacao 提供的建议之后):

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 x 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 并重新启动它之后),而且我无法清除该(某种)内存。

最佳答案

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

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

关于python - 将 'while' 循环中的数据存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com