重塑磁盘阵列以四十个字

2024-06-02 08:15:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想问一下,是否有办法重塑Fortran-contiguous (column-major) order中的dask数组,因为np.reshape函数的并行版本还不受支持(see here)。你知道吗


Tags: 函数版本herenpordercolumn数组dask
1条回答
网友
1楼 · 发布于 2024-06-02 08:15:35

Fortran连续(行主键)顺序是简单的C连续(列主键)顺序。对于dask数组不支持order='F'这一事实,有一个简单的解决方法:

  • 变换数组以反转其维度。你知道吗
  • 把它改成你想要的形状。你知道吗
  • 把它调回原处。你知道吗

在函数中:

def reshape_fortran(x, shape):
    return x.T.reshape(shape[::-1]).T

使用NumPy/dask进行转置基本上是免费的(它不会复制任何数据),因此原则上这个操作也应该非常有效。你知道吗

下面是一个简单的测试来验证它是否正确:

In [48]: import numpy as np

In [49]: import dask.array as da

In [50]: x = np.arange(100).reshape(10, 10)

In [51]: y = da.from_array(x, chunks=5)

In [52]: shape = (2, 5, 10)

In [53]: np.array_equal(reshape_fortran(y, shape).compute(),
    ...:                x.reshape(shape, order='F'))
    ...:
Out[53]: True

相关问题 更多 >