NumPy中order参数C/F/A含义

在阅读NumPy User Guide中3.11节Can you reshape an array?时,看到函数reshape()涉及到一个参数“order”。可选项有C\F\A三项,很疑惑这个参数有什么含义,查阅了相关资料,把收获放在下面吧。

首先看文档中对这个参数的解释:

order: C means to read/write the elements using C-like index order,F means to read/write the elements using Fortran-like index order,A means to read/write the elements in Fortran-like index order if a is Fortran contiguous in memory,C-like order otherwise. (This is an optional parameter and doesn’t need to be specified.)Essentially, C and Fortran orders have to do with how indices correspond to the order the array is stored in memory.In Fortran, when moving through the elements of a two-dimensional array as it is stored in memory, the first index is the most rapidly varying index. As the first index moves to the next row as it changes, the matrix is stored one column at a time. This is why Fortran is thought of as a Column-major language. In C on the other hand, the last index changes the most rapidly. The matrix is stored by rows, making it a Row-major language. What you do for C or Fortran depends on whether it’s more important to preserve the indexing convention or not reorder the data.

order:C表示使用类似C的索引顺序读取/写入元素,F表示使用类似Fortran的索引顺序读取/写入元素,A表示如果数组a在内存中以Fortran形式连续,则以类似Fortran的索引顺序读取/写入元素,否则为C类顺序。 (这是一个可选参数,不需要指定。)本质上,C和Fortran顺序与索引如何与数组在内存中存储的顺序相对应有关。在Fortran中,当移动两个元素时 三维数组存储在内存中时,第一个索引是变化最快的索引。 当第一个索引更改时移动到下一行时,矩阵一次存储一列。 这就是为什么将Fortran视为Column-major language的原因。 另一方面,在C中,最后一个索引变化最快。 矩阵按行存储,使其成为Row-major language。 对C或Fortran的处理方式取决于保留索引约定还是不对数据重新排序更为重要。

大致明白了,order是指定索引与数组元素对应方式的一个参数,也就是决定了数组元素在内存中的存储方式。一共有两种存储方式:Column-major和Row-major。Fortran语言是前者的代表(也就是说Fortran语言是用Column-major方式在内存中存储数据的),C语言是后者的代表。NumPy中设置这个参数,目的是避免不同数据源(尤其是存储方式不同的)数据传递的时候出现存储错误。

下面再看看Column-major和Row-major:

参考资料:

https://blog.csdn.net/lanchunhui/article/details/55656288

你可能感兴趣的:(NumPy中order参数C/F/A含义)