使用嵌套循环和zip的求和

时间:2017-01-04 21:51:54

标签: python numpy for-loop sum nested-loops

我有一个文件:volume_FREQ.dat

# Volume (V)  FREQ    mode
18.1          400.9    1      #| 
18.1          401.3    2      #| 1st Volume: 18.1
18.1          404.2    3      #| 
18.1          505.2    4      #|
19.2          202.4    1           #|  
19.2          203.6    2           #| 2nd Volume: 19.2 
19.2          205.4    3           #|
19.2          199.5    4           #|

在真实文件中有11个卷,每个卷有45个模式。

然后我有这个文件:parameters.dat

# c           d         f        mode
-1.14     -24.70     1297.20     1
-1.24     -22.60     1295.20     2
-1.54     -21.08     1296.20     3
-1.72     -22.4      1298.40     4        

对于这11个卷中的每个卷,其值为P。在以下公式中,这由P(V)表示:每个P的{​​{1}}值都是通过使用Volume的值对modes进行求和来计算的。相应地},cdf

enter image description here

图1.

变量FREQ就是这个列表:

T

真实列表的长度为100。

对于每个 T = [10.0, 30.1, 50.2] 和每个T,其值为V

最终解决方案是以P

之类的文件结束
data.dat

每个输入变量都可以通过# Volume (V) FREQ mode T P 18.1 400.9 1 10.0 x #| 18.1 401.3 2 10.0 x #| 1st Volume: 18.1 18.1 404.2 3 10.0 x #| 18.1 505.2 4 10.0 x #| 19.2 202.4 1 10.0 x #| 19.2 203.6 2 10.0 x #| 2nd Volume: 19.2 19.2 205.4 3 10.0 x #| 19.2 199.5 4 10.0 x #| 18.1 400.9 1 30.1 x #| 18.1 401.3 2 30.1 x #| 1st Volume: 18.1 18.1 404.2 3 30.1 x #| 18.1 505.2 4 30.1 x #| 19.2 202.4 1 30.1 x #| 19.2 203.6 2 30.1 x #| 2nd Volume: 19.2 19.2 205.4 3 30.1 x #| 19.2 199.5 4 30.1 x #| 18.1 300.1 1 50.2 x #| 18.1 305.2 2 50.2 x #| 1st Volume: 18.1 18.1 303.6 3 50.2 x #| 18.1 303.9 4 50.2 x #| 19.2 304.5 1 50.2 x #| 19.2 305.9 2 50.2 x #| 2nd Volume: 19.2 19.2 306.5 3 50.2 x #| 19.2 307.1 4 50.2 x #| 轻松提取:

numpy

但是,在应用公式并循环import numpy as np c, d, f, mode = np.loadtxt('parameters.dat', skiprows = 1).T V, FREQ, mode = np.loadtxt('Volume_FREQ.dat', skiprows = 1).T

时会遇到困难

我可以创建一个modes列表:

VOLUME

然后是嵌套循环+ VOLUME = [19.2, 18.1]

zip

但是,此解决方案并未正确抓取每个卷的 sum_for_each_volume = [] for i_VOLUME in VOLUME: P_CORRECT = [] for j_c1, j_d, j_FREQ, i_T in zip(c1, d, FREQ, T): P = j_FREQ * i_T * (i_VOLUME * j_c1 + j_d) P_CORRECT.append(P) summation = sum(P_CORRECT) sum_for_each_volume.append(summation) ,并且每个FREQS都无法读取所有T元素。

如果你能帮助我,我将不胜感激。

基于@ user7138814的回答:

运行此脚本:

volume

输出如下:

import numpy as np n_volume = 2 n_mode = 4 n_T = 3 c, d, f, mode = np.loadtxt('parameters.dat', skiprows = 1).T V, FREQ, mode = np.loadtxt('Volume_FREQ.dat', skiprows = 1).T T = [10.0, 30.1, 50.2] V = V.reshape(n_volume, n_mode) FREQ = FREQ.reshape(n_volume, n_mode) P_for_each_volume_and_each_T = [] for i in range(n_volume): for j in range(n_T): P = 0 for k in range(n_mode) P += FREQ[i,k] * T[j] * (V[i,k]*c[k] + d[k]) P_for_each_volume_and_each_T.append(P) print 'P = ', P_for_each_volume_and_each_T

但是,使用P = [-830821.31000000006, -2500772.1431000005, -4170722.9762000004, -403382.67200000002, -1214181.8427200001, -2024981.0134400004] 策略,如此处所示(运行以下脚本):

P_for_each_volume_and_each_T[i] = P_for_each_volume_and_each_T[i] + P_for_each_volume_and_each_T[i-1]

你得到这个输出:

import numpy as np n_volume = 2 n_mode = 4 n_T = 3 c, d, f, mode = np.loadtxt('parameters.dat', skiprows = 1).T V, FREQ, mode = np.loadtxt('Volume_FREQ.dat', skiprows = 1).T T = [10.0, 30.1, 50.2] V = V.reshape(n_volume, n_mode) FREQ = FREQ.reshape(n_volume, n_mode) P_for_each_volume_and_each_T = [] for i in range(n_volume): for j in range(n_T): P = 0 for k in range(n_mode): P = FREQ[i,k] * T[j] * (V[i,k]*c[k] + d[k]) print 'FREQ[i,k] = ', FREQ[i,k] print 'V[i,k] = ', V[i,k] print 'c[k] = ', c[k] print 'd[k] = ', d[k] print 'P = ', P P_for_each_volume_and_each_T.append(P) print 'P = ', P_for_each_volume_and_each_T for i in xrange(1,len(P_for_each_volume_and_each_T)): P_for_each_volume_and_each_T[i] = P_for_each_volume_and_each_T[i] + P_for_each_volume_and_each_T[i-1] print 'P after summing= ', P_for_each_volume_and_each_T

这总结了:

P = [-270443.66399999999, -814035.42863999994, -1357627.19328, -110570.88, -332818.34880000004, -555065.81760000007]

因此,P after summing= [-270443.66399999999, -1084479.0926399999, -2442106.2859199997, -2552677.1659199996, -2885495.5147199994, -3440561.3323199996]列表与 @ user7138814 P after summing列表不匹配。

因此,哪种策略是解决此问题的正确策略(参见图1 )?

P 策略+= 策略

1 个答案:

答案 0 :(得分:1)

你还需要一个for循环,因为你有3个不同长度的数组(即你的参数空间是3维)。您从V加载的FREQvolume_FREQ.dat实际上是二维数据。所以使用重塑和另一个循环,你会得到类似的东西:

import numpy as np

n_volume = 11
n_mode = 45
n_T = 3

c, d, f, mode = np.loadtxt('parameters.dat', skiprows = 1).T
V, FREQ, mode = np.loadtxt('Volume_FREQ.dat',  skiprows = 1).T
T =  [10.0, 30.1, 50.2]

V = V.reshape(n_volume, n_mode)
FREQ = FREQ.reshape(n_volume, n_mode)

P_for_each_volume_and_each_T = []
for i in range(n_volume):
    for j in range(n_T):
        P = 0
        for k in range(n_mode)
            P += FREQ[i,k] * T[j] * (V[i,k]*c[k] + d[k])
        P_for_each_volume_and_each_T.append(P)

更多numpythonic将是以下数组操作:

V = V.reshape(n_volume, n_mode)
FREQ = FREQ.reshape(n_volume, n_mode)
T = np.array(T).reshape(-1, 1, 1)
P_for_each_volume_and_each_T = (FREQ * T * (V*c + d)).sum(axis=0)

这会产生(n_volume, n_T) 2D数组。使用ravel获得与for循环相同的结果。