简体   繁体   English

根据另一个numpy数组值计算numpy数组

[英]Calculating numpy array according to another numpy array values

I need to calculate array Z having array D (only using indexing, slicing and broadcasting, NO LOOPS): 我需要计算具有数组D的数组Z(仅使用索引,切片和广播,没有循环):

D = [0, 0, 0, 0, 12, 36, 24, 24, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 36]

Z = [nan, nan, nan, nan, 12., 14.4, 15.36, 16.224, 16.224, 16.224, 16.224, 16.224, 16.224, 16.224, 16.224, 15.8016, 15.8016, 15.8016, 15.8016, 17.8214]

Rule#1: Before first non-zero value of D (here index < 4) => array Z values equal nan (here indices 0 to 3) 规则#1:在D的第一个非零值之前(此处索引<4)=>数组Z值等于nan(此处索引0至3)

Rule#2: First non-zero value of D (here index 4, value 12) => array Z gets the value of A at that index (12) 规则2:D的第一个非零值(此处为索引4,值为12)=>数组Z在该索引处获得A的值(12)

Rule#3: Following Rule#2, if D is not equal to 0 at index i => Z[i] = Z[i-1] + 0.1 * (D[i] - Z[i-1]) 规则3:遵循规则2,如果索引i上的D不等于0 = Z [i] = Z [i-1] + 0.1 *(D [i]-Z [i-1])

ie: 即:

ind=4: D[4]=12 => Z[4]=12 (Rule#2)

ind=5: D[5]=36 => Z[5]=12 + 0.1 * (36 - 12) = 14.4

ind=6: D[6]=24 => Z[6]=14.4 + 0.1 * (24 - 14.4) = 15.36

ind=7: D[7]=24 => Z[7]=15.36 + 0.1 * (24 - 15.36) = 16.224

Rule#4: If D is equal to 0 (here indice i = 8) => Z[i] = Z[i-1] 规则4:如果D等于0(此处索引i = 8)=> Z [i] = Z [i-1]

ie: 即:

ind=8: D[8]=0 =>  D[8]=D[7]=16.224

I hope it can help: 我希望它可以帮助:

def function_needed(D,alpha):
    #Rule 1
    Z=np.zeros(len(D))
    idx=(np.array(D)!=0).argmax(axis=0)
    Z[:idx] = np.NaN
    #Rule 2
    Z[idx]=D[idx]
    #Rule 3
    x=np.array(D)[np.nonzero(np.array(D))]
    n = len(x)
    y_1 = Z[idx]
    pot = (1-alpha)**np.arange(n-1, -1, -1)
    y = alpha*np.cumsum(pot * x)/pot+y_1*(1-alpha)**np.arange(1, n+1)
    Z[np.nonzero(D)]=y
    #Rule 4
    mask =(Z==0)
    idx = np.where(~mask,np.arange(mask.shape[0]),0)
    Z=Z[np.maximum.accumulate(idx,axis=0, out=idx)]
    return Z

testing=function_needed(D,0.1)

I developed the function called function_needed to make all rules once. 我开发了一个名为function_needed的函数,以使所有规则一次。 This is the explanation step by step 这是逐步的解释

Rule 1 规则1

1- Create an array with 0 of the same size of the original array D 1-创建一个与原始数组D大小相同的0数组

Z=np.zeros(len(D))

2- Get the index of the first non zero value 2-获取第一个非零值的索引

idx=(np.array(D)!=0).argmax(axis=0)

3- Put NaN to all values prior to the first non zero value 3-将NaN置于所有第一个非零值之前的值

Z[:idx] = np.NaN

Rule 2 规则二

1- fill Z with the first non zero demand at the index of the first non zero value of D 1-在D的第一个非零值的索引处用第一个非零需求填充Z

Z[idx]=D[idx]

Rule 3 规则三

There is new calculation of the Z only when there is non zero demand 仅当需求非零时才有新的Z计算

1- Create X the array corresponding to D non zero 1-创建X对应于D非零的数组

x=np.array(D)[np.nonzero(np.array(D))]

2- If we consider Y the calculation as proposed in the rule 3 for non zero demand. 2-如果我们考虑Y,则按非规则需求3中的规则进行计算。 We remark that recursively each element of Y is given with the following formula 我们指出,递归地将Y的每个元素赋予以下公式

image of the formula 公式的图像

where y_1 is the first non zero value y_1是第一个非零值

n = len(x)

y_1 = Z[idx]

pot = (1-alpha)**np.arange(n-1, -1, -1)

y = alpha*np.cumsum(pot * x)/pot+y_1*(1-alpha)**np.arange(1, n+1)

Z[np.nonzero(D)]=y

At that stage Z is equal to 在那个阶段Z等于

Z

Rule 4 规则四

Replace the 0 values obtained in the previous step by the first inferior non zero value. 用下一个第一个非零值替换在上一步中获得的0值。 This is what we call foreward filling To do so: 这就是我们所谓的前瞻性填充:

mask =(Z==0)
idx = np.where(~mask,np.arange(mask.shape[0]),0)
Z=Z[np.maximum.accumulate(idx,axis=0, out=idx)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM