计算列表列表的平均值

时间:2018-05-10 11:01:13

标签: python python-2.7 numpy

我有列表包含70000 Vetor(大小为50 int的子列表)

我想重新审核此列表的平均值

我尝试np.mean(列表,轴= 0),但速度太慢了。它花了这么多时间没有任何结果。

计算此平均值的其他任何方式?

我的清单示例:

lis =[[1, 19, 19, 4, 5, 19, 10, 5, 21, 14, 5, 19, 4, 21, 20, 18, 15, 9, 19, 9, 5, 13, 5, 13, 9, 12, 12, 5, 14, 1, 9, 18, 5, 19, 5, 3, 20, 9, 15, 14, 8, 1, 25, 8, 1, 19, 19, 1, 14, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 4, 5, 19, 15, 21, 20, 9, 5, 14, 4, 5, 12, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 16, 18, 9, 14, 3, 5, 13, 12, 25, 8, 9, 3, 8, 1, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 4, 5, 19, 15, 21, 20, 9, 5, 14, 4, 5, 12, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 1, 9, 14, 3, 8, 9, 6, 1, 7, 1, 18, 15, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 19, 15, 21, 20, 9, 5, 14, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 12, 25, 3, 5, 5, 3, 15, 12, 12, 5, 7, 9, 1, 12, 1, 2, 15, 21, 5, 19, 19, 5, 4, 9, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 19, 15, 21, 20, 9, 5, 14, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 1, 2, 4, 18, 1, 8, 13, 1, 14, 5, 2, 14, 15, 21, 26, 9, 4, 1, 14, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 4, 5, 19, 15, 21, 20, 9, 5, 14, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 19, 1, 1, 4, 2, 14, 15, 21, 1, 2, 9, 23, 1, 11, 11, 1, 19, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 4, 5, 19, 15, 21, 20, 9, 5, 14, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 1, 2, 4, 5, 12, 10, 1, 12, 9, 12, 2, 5, 14, 8, 1, 9, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[5, 3, 15, 12, 5, 19, 15, 21, 20, 9, 5, 14, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 19, 1, 11, 9, 14, 1, 2, 9, 14, 20, 5, 12, 8, 15, 21, 3, 5, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 4, 5, 19, 15, 21, 20, 9, 5, 14, 4, 5, 12, 5, 3, 15, 12, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 5, 12, 13, 1, 19, 19, 9, 18, 1, 1, 12, 11, 8, 1, 4, 18, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 19, 19, 19, 15, 21, 20, 9, 5, 14, 5, 3, 15, 12, 5, 4, 5, 18, 5, 21, 19, 19, 9, 20, 5, 5, 3, 15, 12, 5, 12, 1, 12, 12, 1, 25, 1, 3, 15, 21, 20, 13, 12, 25, 18, 1, 3, 8, 9, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],...]
print np.mean(lis,axis=0)

1 个答案:

答案 0 :(得分:4)

这可能会更快,使用itertools.izip,但在你的情况下(如果你不把它投射到numpy数组)理解做得最好:

[(sum(x)*1.0)/len(x) for x in itertools.izip(*lis)]

*1.0归功于python 2.x部门。

lis = [[random.randint(1,50) for j in range(50)] for i in range(1400)]
%timeit np.mean(lis, axis=0)
100 loops, best of 3: 3.67 ms per loop

%timeit [(sum(x)*1.0)/len(x) for x in itertools.izip(*lis)]
100 loops, best of 3: 1.07 ms per loop
%timeit [(sum(x)*1.0)/len(x) for x in lis] #NOTE: equivalent to np.mean(lis,axis=1)
100 loops, best of 3: 660 µs per loop

lisnp = np.array(lis) 

%timeit np.mean(lisnp, axis=0)
100 loops, best of 3: 132 µs per loop

lisnp = np.array(lis, dtype=np.float64)

%timeit np.mean(lisnp, axis=0)
100 loops, best of 3: 67.3 µs per loop