Item assignment to Python dask array objects

天大地大妈咪最大 提交于 2019-12-03 13:46:50

问题


I've created a Python dask array and I'm trying to modify a slice of the array as follows:

import numpy as np
import dask.array as da

x = np.random.random((20000, 100, 100)) # Create numpy array
dx = da.from_array(x, chunks=(x.shape[0], 10, 10)) # Create dask array from numpy array

dx[:50, :, :] = 0 # Modify a slice of the dask array

Such an attempt to modify the dask array raises the exception:

TypeError: 'Array' object does not support item assignment

Is there a way to modify a dask array slice without raising an exception?


回答1:


Currently dask.array does not support item assignment or any other mutation operation.

In the case above I recommend concatenating with zeros

In [1]: import dask.array as da

In [2]: dx = da.random.random((20000 - 50, 100, 100), chunks=(None, 10, 10))

In [3]: z = da.zeros((50, 100, 100), chunks=(50, 10, 10))

In [4]: dx2 = da.concatenate([z, dx], axis=0)

In [5]: dx2
Out[5]: dask.array<concate..., shape=(20000, 100, 100), dtype=float64, chunksize=(50, 10, 10)>

In [6]: (dx2 == 0)[0:100, 0, 0].compute()
Out[6]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False, False], dtype=bool)

The da.where(condition, iftrue, iffalse) function can also be quite useful in working around cases where mutation is often desired.



来源:https://stackoverflow.com/questions/36142892/item-assignment-to-python-dask-array-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!