gpt4 book ai didi

python - 如何用大字典映射 dask 系列

转载 作者:行者123 更新时间:2023-11-28 17:06:28 25 4
gpt4 key购买 nike

我正在尝试找出使用大型映射来映射 dask Series 的最佳方法。直白的series.map(large_mapping)问题 UserWarning: Large object of size <X> MB detected in task graph并建议使用 client.scatterclient.submit但后者并没有解决问题,实际上它要慢得多。尝试 broadcast=Trueclient.scatter也没有帮助。

import argparse
import distributed
import dask.dataframe as dd

import numpy as np
import pandas as pd


def compute(s_size, m_size, npartitions, scatter, broadcast, missing_percent=0.1, seed=1):
np.random.seed(seed)
mapping = dict(zip(np.arange(m_size), np.random.random(size=m_size)))
ps = pd.Series(np.random.randint((1 + missing_percent) * m_size, size=s_size))
ds = dd.from_pandas(ps, npartitions=npartitions)
if scatter:
mapping_futures = client.scatter(mapping, broadcast=broadcast)
future = client.submit(ds.map, mapping_futures)
return future.result()
else:
return ds.map(mapping)


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-s', default=200000, type=int, help='series size')
parser.add_argument('-m', default=50000, type=int, help='mapping size')
parser.add_argument('-p', default=10, type=int, help='partitions number')
parser.add_argument('--scatter', action='store_true', help='Scatter mapping')
parser.add_argument('--broadcast', action='store_true', help='Broadcast mapping')
args = parser.parse_args()

client = distributed.Client()
ds = compute(args.s, args.m, args.p, args.scatter, args.broadcast)
print(ds.compute().describe())

最佳答案

你的问题在这里

In [4]: mapping = dict(zip(np.arange(50000), np.random.random(size=50000)))

In [5]: import pickle

In [6]: %time len(pickle.dumps(mapping))
CPU times: user 2.24 s, sys: 18.6 ms, total: 2.26 s
Wall time: 2.25 s
Out[6]: 6268809

因此 mapping 很大且未分区 - 在这种情况下,分散操作会给您带来问题。

考虑替代方案

def make_mapping():
return dict(zip(np.arange(50000), np.random.random(size=50000)))

mapping = client.submit(make_mapping) # ships the function, not the data
# and requires no serialisation
future = client.submit(ds.map, mapping)

这不会显示警告。然而,在这里使用字典来做映射对我来说似乎很奇怪,一系列直数组似乎更好地编码了数据的性质。

关于python - 如何用大字典映射 dask 系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50638682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com