【发布时间】:2020-05-25 00:05:22
【问题描述】:
我有一个有趣的性能优化问题,目前是我们应用程序的瓶颈
鉴于 DataFrame 具有非唯一时间戳 index、id 和 weight 列(事件)和一系列时间戳(观察),我必须为每个观察分配一个随机事件 id,该事件发生在给定时间戳考虑权重。时间戳被限制到最接近的分钟,并且可以被视为从某个开始日期时间开始的分钟数。
测试数据生成:
import pandas as pd
import numpy as np
import random
from datetime import datetime as dt, timedelta as td
# typical date range is one month
start = dt(2020, 2, 1, 0, 0, 0)
end = dt(2020, 3, 1, 0, 0, 0)
# generate one event per minute
index = pd.date_range(start, end, freq='1min')
N = len(index)
events = pd.DataFrame({'id': np.arange(N), 'weight': np.random.random(N)}, index=index)
# generate some random events to simulate index duplicates
random_minutes = pd.to_datetime([start + td(minutes=random.randint(0, N)) for m in range(3*N)])
random_events = pd.DataFrame({'id': np.arange(3*N), 'weight': np.random.random(3*N)}, index=random_minutes)
events = pd.concat([events, random_events])
# observations, usually order or two orders of magnitude more records than events
observations = pd.Series([start + td(minutes=random.randint(0, N)) for m in range(10*N)])
样本数据点
>>> print(events.sort_index().to_string())
id weight
2020-02-09 01:00:00 0 0.384927
2020-02-09 01:00:00 15 0.991314
2020-02-09 01:00:00 17 0.098999
2020-02-09 01:01:00 1 0.813859
2020-02-09 01:01:00 2 0.922601
2020-02-09 01:01:00 1 0.738795
2020-02-09 01:02:00 2 0.898842
2020-02-09 01:02:00 13 0.621904
2020-02-09 01:03:00 12 0.075857
2020-02-09 01:03:00 3 0.135762
2020-02-09 01:03:00 9 0.398885
...
>>> print(observations.sort_values().to_string())
12 2020-02-09 01:00:00
9 2020-02-09 01:00:00
44 2020-02-09 01:00:00
31 2020-02-09 01:01:00
53 2020-02-09 01:02:00
3 2020-02-09 01:02:00
6 2020-02-09 01:03:00
我目前最快的解决方案是通过索引groupby 事件,为每个记住样本的组函数返回。很难对其进行正确矢量化,因为每个组的许多记录可能会有所不同,并且我必须根据权重返回 ID。
%%timeit
from functools import partial
# create a per-minute random function returning id according to weights
randomizers = events.groupby(level=0).apply(
lambda s: partial(
np.random.choice,
s.id.values,
p=s.weight.values/s.weight.sum()
)
)
# for each observation, find random generator and call it
selections = randomizers.loc[observations].apply(lambda f: f())
14.7 s ± 49.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
所以我的问题是,有没有一种更好、更快的方法来做我需要做的事情?我面临的主要问题:
- 每分钟可以有多个事件,每个事件都有 ID 和概率
- 每分钟的事件数是随机的,一分钟可以有 1 个,不同的可以有 20 个
- 对于每个观察,我需要单独选择一个随机选项。
有什么想法吗?我正在考虑使用 numba,但也许有一些聪明的解决方案?
【问题讨论】:
标签: python pandas optimization pandas-groupby