根据这一讨论
(
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)
# 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