我已经创建了一个Python dask数组,我试图按如下方式修改数组:
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
这种修改dask数组的尝试引发了异常:
TypeError: 'Array' object does not support item assignment
有没有办法在不引发异常的情况下修改dask数组切片?
答案 0 :(得分:3)
目前dask.array不支持项目分配或任何其他变异操作。
在上面的例子中,我建议用零连接
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)
da.where(condition, iftrue, iffalse)
函数在处理通常需要突变的情况时也非常有用。