numpy 中列表列表的平均值

Yoh*_*oth 1 python numpy

我有一个循环,可以获取列表列表:

for i in range(num_exp):
  li = func()
Run Code Online (Sandbox Code Playgroud)

其中li是表单列表的列表 [["s1", 1, 2], ["s2", 2, 3], ["s3", 3, 4]] (第一项是字符串,其余 2 项是数字)

li我想在循环中对每个值进行平均。所以对于num_exp = 3和李的

[["s1", 1, 2], ["s2", 3, 4], ["s3", 5, 6]]
[["s1", 2, 3], ["s2", 4, 5], ["s3", 6, 7]]
[["s1", 3, 4], ["s2", 5, 6], ["s3", 7, 8]]
Run Code Online (Sandbox Code Playgroud)

我会得到

[["s1", 6/3, 9/3], ["s2", 12/3, 15/3], ["s3", 18/3, 21/3]]
Run Code Online (Sandbox Code Playgroud)

我想用 numpy 来做。在简单的Python中我做如下

 dic = {}
 for l in li:
     if l[0] not in dic:
        dic[l[0]] = l[1:]
      else:
        dic[l[0]][0] += l[1] 
        dic[l[0]][1] += l[2] 

 fl = []
 for m in dic:
    fl.append([m, dic[m][0]/num_exp, dic[m[1]/num_exp])
Run Code Online (Sandbox Code Playgroud)

但似乎效率很低

And*_* L. 5

从指定np.array列表的列表中创建并将其分组到同一组中。切片轴 2(最右侧轴)上的最后 2 个元素,然后将其除以。最后,它的唯一字符串值。lidtype='object'swapaxesssumnum_expcolumn_stack

num_exp = 3
li = [[["s1", 1, 2], ["s2", 3, 4], ["s3", 5, 6]],
      [["s1", 2, 3], ["s2", 4, 5], ["s3", 6, 7]],
      [["s1", 3, 4], ["s2", 5, 6], ["s3", 7, 8]]]

arr = np.array(li, dtype='object').swapaxes(0, 1)

Out[372]:
array([[['s1', 1, 2],
        ['s1', 2, 3],
        ['s1', 3, 4]],

       [['s2', 3, 4],
        ['s2', 4, 5],
        ['s2', 5, 6]],

       [['s3', 5, 6],
        ['s3', 6, 7],
        ['s3', 7, 8]]], dtype=object)

arr1 = arr[...,[1,2]].sum(axis=1) / num_exp

Out[380]:
array([[2.0, 3.0],
       [4.0, 5.0],
       [6.0, 7.0]], dtype=object)

s = arr[:,0, 0]
result = np.column_stack([s, arr1])

Out[389]:
array([['s1', 2.0, 3.0],
       ['s2', 4.0, 5.0],
       ['s3', 6.0, 7.0]], dtype=object)
Run Code Online (Sandbox Code Playgroud)