3

我试图用相同的键找到几个字典的平均值(字典的数量将取决于用户的选择)。每个键都是一个维度的 numpy 数组。

我使用这种方法得到了解决方案

ipython 笔记本查看器

我使用的功能是

def metaa(lis,name):
    x = len(lis)
    pr=""
    for i in xrange(x):
        if i == 0:
            pr = pr+name+"["+str(i)+"][x]"
        else:
            pr = pr+"+"+name+"["+str(i)+"][x]"
    pr = "("+pr+")/"+str(x)                 
    return pr

我创建了这样的字典。

import numpy as np
a1 = np.random.randint(100,size=(3,10))
a2 = np.random.randint(100,size=(3,10))
a3 = np.random.randint(100,size=(3,10))
al=[a1,a2,a3]
dicta = {'a1':a1,'a2':a2,'a3':a3}
dictb = {'a1':a1,'a2':a2,'a3':a3}
R = [dicta,dictb]

我在两个字典中使用相同的值进行测试。我这样调用函数。

Res = {}
for x in R[0]:
    Res[x] = eval(metaa(R,'R'))

我认为这种方法很hackish,有没有更好的方法来解决这个问题?

4

1 回答 1

3

构建一个字符串来评估它不是很优雅。更好地与 ,reduce结合使用,所有这些都由 list和 dict comprehensionsnp.add启用。首先,将字典列表转换为列表字典:[]{}RS

S = {k:[ R[j][k] for j in range(len(R)) ] for k in R[0].keys()}

现在,每个键只有一个“裸”numpy数组列表,可以使用添加np.add然后除以单个列表的长度:

S = {'a1': [array([[ 32, 120,  80, 380, 360, 212, 188,  56, 312, 112],
                   [388, 348, 196, 236,  60, 200, 224, 208,  24, 104],
                   [324, 296,  24, 52, 220,  12, 104,  52, 232, 196]]),
            array([[ 32, 120,  80, 380, 360, 212, 188,  56, 312, 112],
                   [388, 348, 196, 236,  60, 200, 224, 208,  24, 104],
                   [324, 296,  24, 152, 220,  12, 104,  52, 232, 196]])],
     'a2': [array([[30, 82, 99, 72, 79, 98, 93, 93, 28, 46],
                   [ 8, 17, 50, 59, 85, 73, 48, 97, 87, 41],
                   [98, 36, 27, 55, 98, 39, 73, 51, 27, 33]]),
            array([[30, 82, 99, 72, 79, 98, 93, 93, 28, 46],
                   [ 8, 17, 50, 59, 85, 73, 48, 97, 87, 41],
                   [98, 36, 27, 55, 98, 39, 73, 51, 27, 33]])],
     'a3': [array([[78, 24, 87, 83, 30, 14, 88, 57, 55, 73],
                   [76, 94, 99, 58, 63, 34, 70, 81, 45, 20],
                   [32, 61,  0,  3, 33, 33, 38, 90, 11,  3]]),
            array([[78, 24, 87, 83, 30, 14, 88, 57, 55, 73],
                   [76, 94, 99, 58, 63, 34, 70, 81, 45, 20],
                   [32, 61,  0,  3, 33, 33, 38, 90, 11,  3]])]}

计算平均值:

T = {k:( reduce(np.add, v)/len(v) ) for k,v in S.iteritems()}

现在T是具有平均值的 numpy 数组的字典:

T = {'a1': array([[ 32, 120,  80, 380, 360, 212, 188,  56, 312, 112],
                  [388, 348, 196, 236,  60, 200, 224, 208,  24, 104],
                  [324, 296,  24, 152, 220,  12, 104,  52, 232, 196]]),
     'a2': array([[30, 82, 99, 72, 79, 98, 93, 93, 28, 46],
                  [ 8, 17, 50, 59, 85, 73, 48, 97, 87, 41],
                  [98, 36, 27, 55, 98, 39, 73, 51, 27, 33]]),
     'a3': array([[78, 24, 87, 83, 30, 14, 88, 57, 55, 73],
                  [76, 94, 99, 58, 63, 34, 70, 81, 45, 20],
                  [32, 61,  0,  3, 33, 33, 38, 90, 11,  3]])}
于 2013-10-29T09:58:32.847 回答