之前的回答由LCT https://stackoverflow.com/users/9974926/lct涵盖了这里应该说的大部分内容,however我认为可以合并dask='parallelized'
具有多个输出,就像您从中获得的那样scipy.stats.linregress
.
这里的技巧是将多个输出堆叠到一个数组中,然后输出它,您还必须使用output_core_dims
kwarg 指定 DataArray 输出apply_ufunc()
调用现在将有一个额外的维度:
def new_linregress(x, y):
# Wrapper around scipy linregress to use in apply_ufunc
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
return np.array([slope, intercept, r_value, p_value, std_err])
# return a new DataArray
stats = xr.apply_ufunc(new_linregress, ds[x], ds[y],
input_core_dims=[['year'], ['year']],
output_core_dims=[["parameter"]],
vectorize=True,
dask="parallelized",
output_dtypes=['float64'],
output_sizes={"parameter": 5},
)
N.B.该方法目前仅适用于dask='parallelized'
如果你有dask<2.0
,但如果你有其他类似的东西,它似乎适用于多个输出dask='allowed'
。看看这个Github问题 https://github.com/pydata/xarray/issues/3574了解更多详情。
希望能帮助到你!
Edit:我已获悉dask<2.0
只要您有,问题就已得到纠正xarray>=0.15.0
!所以可以使用dask='parallelized'
现在要加快速度。 :)