Python dask.array.apply_沿_轴:使用dask.array的每一行作为另一个函数的输入失败,因为有额外的元素([1])

Python dask.array.apply_沿_轴:使用dask.array的每一行作为另一个函数的输入失败,因为有额外的元素([1]),python,numpy,dask,Python,Numpy,Dask,我有一个大数组(arr)的形状(620000000,2),每行代表两个整数索引,我想传递给另一个函数。结构有点像这样: def myfunc(a, b): return a + b def pair_func(two_elem_arr): # takes a 2-element vector/array return myfunc(*two_elem_arr) 我尝试使用dask来并行化整个过程,但出现以下问题 当使用apply\u沿_轴仅打印时,生成的背景循环的第一个元素是

我有一个大数组(
arr
)的形状
(620000000,2)
,每行代表两个整数索引,我想传递给另一个函数。结构有点像这样:

def myfunc(a, b):
    return a + b
def pair_func(two_elem_arr):  # takes a 2-element vector/array
    return myfunc(*two_elem_arr)
我尝试使用
dask
来并行化整个过程,但出现以下问题

当使用
apply\u沿_轴
仅打印时,生成的背景循环的第一个元素是一个神秘的
[1]
。使用plain
numpy
时,完全相同的过程非常有效:

import numpy as np
import dask.array as da

arr = np.repeat(np.arange(10), 2).reshape((10, 2))
darr = da.from_array(arr)

print('numpy version:')
np.apply_along_axis(arr=arr, axis=1, func1d=print)
print('\ndask version:')
da.apply_along_axis(arr=darr, axis=1, func1d=print).compute()
输出:

numpy version:
[0 0]
[1 1]
[2 2]
[3 3]
[4 4]
[5 5]
[6 6]
[7 7]
[8 8]
[9 9]

dask version:
[1]   <------------- ?
[0 0]
[1 1]
[2 2]
[3 3]
[4 4]
[5 5]
[6 6]
[7 7]
[8 8]
[9 9]
da.apply_沿_轴调用会导致以下异常:

TypeError: myfunc() missing 1 required positional argument: 'b'
更让我困惑的是,整个过程与其他精细的函数一起工作,如
da.sum
(以及
np.sum
):

那么实际上有两个问题,

  • 为什么
    [1]
    da的输出中有一个
    [1]
    。沿轴(…)应用\u以及如何删除它
  • 如果没有,是否有其他方法可以达到预期的结果

提前感谢

解决方案隐藏在中

[……]

注释

如果未提供数据类型或形状,Dask将尝试通过 在虚拟数组上调用func1d。这可能会产生不正确的数据类型或形状值, 因此,我们建议提供它们。 [……]

当我提供
shape=(1,)
dtype='int'
时,它工作得很好。 在我看来,额外的
[1]
似乎是使用虚拟数组确定
dtype
shape
的结果。我仍然认为不应该是这样

da.apply_along_axis(arr=darr, axis=1, func1d=pair_func, shape=(1,), dtype='int')
da.apply_along_axis(arr=darr, axis=1, func1d=da.sum).compute()
da.apply_along_axis(arr=darr, axis=1, func1d=pair_func, shape=(1,), dtype='int')