使用numpy
,我可以使用2D“掩模”来子集化3D阵列。同样返回一个带有dask数组的IndexError
。是否有任何方法可以使用dask
再现下面的numpy
行为?
import numpy as np
import dask.array as da
# Create 3d arrays of random values and mask with shape matching second and third dimensions
y_da = da.random.random(size=(20, 100, 100))
y_np = np.random.rand(20, 100, 100)
mask = np.zeros((100, 100), dtype=np.uint8)
mask[20:80, 3:77] = 1
# Apply mask (flattens axes 1 and 2)
print(y_np[:,mask == 1].shape) # OK
print(y_da[:,mask == 1].shape) # IndexError
字符串
1条答案
按热度按时间wbrvyc0a1#
字符串
在
numpy
中,使用布尔掩码通常等同于使用相应的“高级索引”数组。因此:型
参见基本索引文档的布尔索引部分:
https://numpy.org/doc/stable/user/basics.indexing.html#advanced-indexing
您可以将相同的想法应用于
dask
。我必须将
nonzero
的结果与I,J
进行“拆分”,否则:型