将一列随机数添加到DaskDataFrame的正确方法

Correct way to add a column of random numbers to a dask dataframe(将一列随机数添加到DaskDataFrame的正确方法)
本文介绍了将一列随机数添加到DaskDataFrame的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一列随机数添加到DaskDataFrame的正确方法是什么?我显然可以使用map_partitions将列添加到每个分区,但我不确定当Dask并行化该计算时如何处理随机状态。(即,它是否会在所有工作进程中使用相同的随机状态,从而在每个工作进程中生成相同的随机数?)

dask.array.random(https://docs.dask.org/en/latest/_modules/dask/array/random.html)中似乎有相关函数,但我找不到如何将这些函数与DaskDataFrame一起使用的示例。

推荐答案

根据此讨论 (https://github.com/dask/distributed/issues/2558),不需要设置/跟踪numpy种子,推荐的方法是使用dask.array(问题中提到过)。也许,实现可重现随机性的最佳途径是创建dask.array并转换为dask.dataframe

import dask.array as da

# this is not reproducible
for _ in range(3):
    x = da.random.random((10, 1), chunks=(2, 2))
    print(x.sum().compute())

# this is reproducible
for _ in range(3):
    state = da.random.RandomState(1234)
    y = state.random(size=(10,1), chunks=(2,2))
    print(y.sum().compute())

# conver to ddf
import dask.dataframe as dd
ddf = dd.from_dask_array(y, columns=['A'])

# if there's another existing dataframe ddf2
ddf2 = dd.from_pandas(pd.DataFrame(range(10), columns=['B']), npartitions=2)
ddf2

# then simple column assignment will work even if partitions are not aligned
ddf2['A'] = ddf['A']
print((ddf.compute() == ddf2[['A']].compute()).sum() == len(ddf))

# of course it will be more efficient to have partitions align
# you can inspect the DAG with ddf2.visualize() to see why
# also note carefully that the lengths of ddf and ddf2 should match
# otherwise there might be unexpected situations downstream
# to see why, try changing the size of `y` above and then compare
# ddf and ddf2

这篇关于将一列随机数添加到DaskDataFrame的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)