【问题标题】:Python: Improving long cumulative sumPython:改进长累积和
【发布时间】:2010-05-30 03:06:33
【问题描述】:

我有一个对大量实验数据进行操作的程序。数据存储为对象列表,这些对象是具有以下属性的类的实例:

  • time_point - 采样时间
  • cluster - 从中​​提取样本的节点集群的名称
  • node - 从中​​提取样本的节点的名称
  • qty1 = 第一个数量的样本值
  • qty2 = 第二个数量的样本值

我需要从数据集中导出一些值,以三种方式分组 - 一次用于整个样本,一次用于每个节点集群,一次用于每个节点。我需要导出的值取决于 qty1 和 qty2 的(按时间排序的)累积总和:qty1 和 qty2 的累积总和的元素总和的最大值,最大值出现的时间点,以及qty1 和 qty2 在那个时间点的值。

我想出了以下解决方案:

dataset.sort(key=operator.attrgetter('time_point'))

# For the whole set
sys_qty1 = 0
sys_qty2 = 0
sys_combo = 0
sys_max = 0

# For the cluster grouping
cluster_qty1 = defaultdict(int)
cluster_qty2 = defaultdict(int)
cluster_combo = defaultdict(int)
cluster_max = defaultdict(int)
cluster_peak = defaultdict(int)

# For the node grouping
node_qty1 = defaultdict(int)
node_qty2 = defaultdict(int)
node_combo = defaultdict(int)
node_max = defaultdict(int)
node_peak = defaultdict(int)

for t in dataset:
  # For the whole system ######################################################
  sys_qty1 += t.qty1
  sys_qty2 += t.qty2
  sys_combo = sys_qty1 + sys_qty2
  if sys_combo > sys_max:
    sys_max = sys_combo
    # The Peak class is to record the time point and the cumulative quantities
    system_peak = Peak(time_point=t.time_point,
                       qty1=sys_qty1,
                       qty2=sys_qty2)
  # For the cluster grouping ##################################################
  cluster_qty1[t.cluster] += t.qty1
  cluster_qty2[t.cluster] += t.qty2
  cluster_combo[t.cluster] = cluster_qty1[t.cluster] + cluster_qty2[t.cluster]
  if cluster_combo[t.cluster] > cluster_max[t.cluster]:
    cluster_max[t.cluster] = cluster_combo[t.cluster]
    cluster_peak[t.cluster] = Peak(time_point=t.time_point,
                                   qty1=cluster_qty1[t.cluster],
                                   qty2=cluster_qty2[t.cluster])
  # For the node grouping #####################################################
  node_qty1[t.node] += t.qty1
  node_qty2[t.node] += t.qty2
  node_combo[t.node] = node_qty1[t.node] + node_qty2[t.node]
  if node_combo[t.node] > node_max[t.node]:
    node_max[t.node] = node_combo[t.node]
    node_peak[t.node] = Peak(time_point=t.time_point,
                             qty1=node_qty1[t.node],
                             qty2=node_qty2[t.node])

这会产生正确的输出,但我想知道它是否可以变得更具可读性/Pythonic,和/或更快/更具可扩展性。

上面的内容很有吸引力,因为它只遍历(大)数据集一次,但没有吸引力,因为我基本上复制/粘贴了相同算法的三个副本。

为了避免上述的复制/粘贴问题,我也尝试了这个:

def find_peaks(level, dataset):

  def grouping(object, attr_name):
    if attr_name == 'system':
      return attr_name
    else:
      return object.__dict__[attrname]

  cuml_qty1 = defaultdict(int)
  cuml_qty2 = defaultdict(int)
  cuml_combo = defaultdict(int)
  level_max = defaultdict(int)
  level_peak = defaultdict(int)

  for t in dataset:
    cuml_qty1[grouping(t, level)] += t.qty1
    cuml_qty2[grouping(t, level)] += t.qty2
    cuml_combo[grouping(t, level)] = (cuml_qty1[grouping(t, level)] +
                                      cuml_qty2[grouping(t, level)])
    if cuml_combo[grouping(t, level)] > level_max[grouping(t, level)]:
      level_max[grouping(t, level)] = cuml_combo[grouping(t, level)]
      level_peak[grouping(t, level)] = Peak(time_point=t.time_point,
                                            qty1=node_qty1[grouping(t, level)],
                                            qty2=node_qty2[grouping(t, level)])
  return level_peak

system_peak = find_peaks('system', dataset)
cluster_peak = find_peaks('cluster', dataset)
node_peak = find_peaks('node', dataset)

对于(未分组的)系统级计算,我也想出了这个,很漂亮:

dataset.sort(key=operator.attrgetter('time_point'))

def cuml_sum(seq):
  rseq = []
  t = 0
  for i in seq:
    t += i
    rseq.append(t)
  return rseq

time_get = operator.attrgetter('time_point')
q1_get = operator.attrgetter('qty1')
q2_get = operator.attrgetter('qty2')

timeline = [time_get(t) for t in dataset]
cuml_qty1 = cuml_sum([q1_get(t) for t in dataset])
cuml_qty2 = cuml_sum([q2_get(t) for t in dataset])
cuml_combo = [q1 + q2 for q1, q2 in zip(cuml_qty1, cuml_qty2)]

combo_max = max(cuml_combo)
time_max = timeline.index(combo_max)
q1_at_max = cuml_qty1.index(time_max)
q2_at_max = cuml_qty2.index(time_max)

然而,尽管这个版本对列表推导和 zip() 的使用很酷,但它循环数据集 3 次只是为了系统级计算,我想不出一个好的方法来做集群级和节点级计算而不做一些缓慢的事情,例如:

timeline = defaultdict(int)
cuml_qty1 = defaultdict(int)
#...etc.

for c in cluster_list:
  timeline[c] = [time_get(t) for t in dataset if t.cluster == c]
  cuml_qty1[c] = [q1_get(t) for t in dataset if t.cluster == c]
  #...etc.

Stack Overflow 有没有人提出改进建议?上面的第一个 sn-p 对于我的初始数据集(大约一百万条记录)运行良好,但后来的数据集将有更多的记录和集群/节点,因此可扩展性是一个问题。

这是我对 Python 的第一次重要的使用,我想确保我能够适当地利用该语言(这是替换一组非常复杂的 SQL 查询,而早期版本的 Python 版本本质上是对所做的事情的直接翻译非常低效)。我通常不会做太多编程,所以我可能会遗漏一些基本的东西。

非常感谢!

【问题讨论】:

  • 您可以先进行所有节点计算,然后使用节点结果计算集群结果,然后使用集群结果计算系统范围的结果。这至少会减少你目前正在做的一些重复(相同的添加)。
  • 感谢您的建议。但是,集群峰值可能与任何单个节点的峰值不同。例如,它们可能同时达到一个中等值,从而导致集群出现巨大的峰值,但对于任何单个节点来说都不是巨大的峰值。

标签: python list-comprehension


【解决方案1】:

这似乎是应用一点面向对象的经典机会。我建议将派生数据设为一个类,并将累积和计算抽象为适用于该类的东西。

类似:

class DerivedData(object):
    def __init__(self):
        self.qty1 = 0.0
        self.qty2 = 0.0
        self.combo = 0.0
        self.max = 0.0
        self.peak = Peak(time_point=0.0, qty1=0.0, qty2=0.0)

    def accumulate(self, data):
        self.qty1 += data.qty1
        self.qty2 += data.qty2
        self.combo = self.qty1 + self.qty2
        if self.combo > self.max:
            self.max = self.combo
            self.peak = Peak(time_point=data.time_point,
                             qty1=self.qty1,
                             qty2=self.qty2)

sys = DerivedData()
clusters = defaultdict(DerivedData)
nodes = defaultdict(DerivedData)

dataset.sort(key=operator.attrgetter('time_point'))

for t in dataset:
    sys.accumulate(t)
    clusters[t.cluster].accumulate(t)
    nodes[t.node].accumulate(t)

这个解决方案抽象出了寻找峰值的逻辑,但仍然只遍历数据集一次。

【讨论】:

  • 彼得,非常感谢。这当然看起来好多了。我会试一试,看看效果如何。我应该提到,所有时间和数据值都保证是整数(实际上每个级别的每个数量的总和都等于零)。
猜你喜欢
  • 1970-01-01
  • 2018-02-28
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 2016-07-26
相关资源
最近更新 更多