import numpy as np
def average(values, aggr_step=24):
keys = np.array(values.keys())
bins = range(aggr_step,1+keys.max(),aggr_step)
npd = np.digitize(keys,bins)
return {b:np.mean(np.r_[[values[k] for k in keys[npd==i]]],axis=0)
for i,b in enumerate(bins)}
抽出一些样本值,并测试:
values = {h:np.array([np.random.random_integers(1,10,5) for _ in 'x'*2])
for h in np.r_[0:100:6]}
{0: array([[ 1, 2, 10, 8, 7],
[10, 1, 8, 2, 5]]),
6: array([[ 7, 10, 7, 6, 4],
[ 9, 4, 6, 1, 1]]),
12: array([[ 7, 10, 10, 5, 4],
[ 7, 6, 2, 6, 7]]),
18: array([[9, 9, 8, 5, 8],
[8, 9, 6, 1, 2]]),
24: array([[8, 1, 5, 9, 7],
[1, 1, 6, 8, 3]]),
30: array([[ 7, 2, 8, 3, 4],
[ 5, 10, 5, 6, 5]]),
36: array([[ 6, 5, 9, 4, 5],
[ 6, 10, 8, 10, 10]]),
42: array([[ 2, 2, 6, 6, 9],
[ 5, 7, 4, 8, 10]]),
48: array([[10, 1, 1, 1, 8],
[ 5, 6, 4, 5, 8]]),
54: array([[ 6, 2, 6, 6, 6],
[10, 6, 9, 4, 8]]),
60: array([[ 6, 8, 2, 1, 7],
[ 1, 10, 10, 2, 3]]),
66: array([[4, 8, 7, 1, 3],
[1, 3, 8, 4, 2]]),
72: array([[ 8, 9, 8, 6, 7],
[ 1, 4, 2, 10, 1]]),
78: array([[ 2, 1, 3, 8, 9],
[ 2, 8, 6, 10, 3]]),
84: array([[ 1, 8, 9, 4, 3],
[ 9, 10, 8, 3, 4]]),
90: array([[ 4, 4, 2, 8, 6],
[ 1, 3, 10, 2, 6]]),
96: array([[ 9, 9, 8, 4, 4],
[ 2, 8, 10, 3, 5]])}
average(values,24)
{24: array([[ 6. , 7.75, 8.75, 6. , 5.75],
[ 8.5 , 5. , 5.5 , 2.5 , 3.75]]),
48: array([[ 5.75, 2.5 , 7. , 5.5 , 6.25],
[ 4.25, 7. , 5.75, 8. , 7. ]]),
72: array([[ 6.5 , 4.75, 4. , 2.25, 6. ],
[ 4.25, 6.25, 7.75, 3.75, 5.25]]),
96: array([[ 3.75, 5.5 , 5.5 , 6.5 , 6.25],
[ 3.25, 6.25, 6.5 , 6.25, 3.5 ]])}