【发布时间】:2013-08-20 18:18:52
【问题描述】:
我在一个文本文件中存储了超过 6500 万个数值。我需要计算最大值、最小值、平均值、标准差以及 25、50 和 75 个百分位数。
通常我会使用附加的代码,但我需要一种更有效的方法来计算这些指标,因为我无法将所有值 p 存储在一个列表中。如何在 Python 中更有效地计算这些值?
import numpy as np
np.average(obj)
np.min(mylist)
np.max(mylist)
np.std(mylist)
np.percentile(obj, 25)
np.percentile(obj, 50)
np.percentile(obj, 75)
maxx = float('-inf')
minx = float('+inf')
sumz = 0
for index, p in enumerate(open("foo.txt", "r")):
maxx = max(maxx, float(p))
minx = min(minx, float(p))
sumz += float(p)
index += 1
my_max = maxx
my_min = minx
my_avg = sumz/index
【问题讨论】:
-
但是问题是什么?
-
我认为不使用内存很难计算百分位数...
-
“6500000 百万点” - 不太可能。您没有多 TB 的数据文件。
-
“数值”的范围和精度是多少?
-
传感器必须有最大有效位数。如果是这样,您可以将它们规范化为整数并使用像 collections.Counter 这样的东西,它可能比 65M 浮点数列表更有效。精度超过 5 或 6 位有效数字的传感器很少见。
标签: python performance statistics memory-efficient