计算多个numpy数组的连续逐步差异

时间:2016-10-17 20:29:41

标签: python arrays numpy

有没有人知道如何计算连续的逐步差异 多个numpy数组。更确切地说,我想总结一下差异 1,2和3然后总结差异2,3和4等。最后, 最高差值和应存储在diff_sum中。下面你找到我的测试 数据和我的代码。我希望你明白我想做什么,否则请联系我,我试着以更好的方式解释我的问题。

#import modules
import numpy as np
#define the test data
#initialise numpy test arrays
sh_20121001 = np.array([0.0]) #day 1

sh_20121002 = np.array([0.2]) #day 2

sh_20121003 = np.array([0.4]) #day 3

sh_20121004 = np.array([0.6]) #day 4

sh_20121005 = np.array([0.2]) #day 5

sh_20121006 = np.array([0.0]) #day 6

sh_20121007 = np.array([0.0]) #day 7
#initialise a list
filelist = [], [], [], [], [], [], [] 
#append the numpy array into a filelist
filelist[0].append(sh_20121001)
filelist[1].append(sh_20121002)
filelist[2].append(sh_20121003)
filelist[3].append(sh_20121004)
filelist[4].append(sh_20121005)
filelist[5].append(sh_20121006)
filelist[6].append(sh_20121007)

#intitialse an array to store the value from the day before
value_day_before = np.zeros([1,]) 
#initialise an arrays for diff 1
diff1 = np.zeros([1,])
#initialise an array for diff 2
diff2 = np.zeros([1,])
#initilise an array fore diff 3
diff3 = np.zeros([1,])
#initialise an array to store consecutive sum of three differences
diff_sum = np.zeros([1,])
#define counter
counter = 0
#start while loop
for i in range(7):
    #read values from filelist
    value = filelist[i][0]
    if counter == 0:
        #calc difference 1
        diff1[0] = value[0] - value_day_before[0]
    if counter == 1:
        #calc difference 2
        diff2[0] = value[0] - value_day_before[0]
    if counter == 2:
        #calc difference 3
        diff3[0] = value[0] - value_day_before[0]
    #sum the first three differences
    diff_sum = diff1 + diff2 + diff3
    #store the current value
    value_day_before = value
    #raise counter
    counter += 1
#END CODE
#my result should be the maximum sum of all consecutive calculated differences sums and thus 
#my result should be:
#diff_sum
#array([ 0.6])

0 个答案:

没有答案