In what order does column-first flattening visit a matrix?
ravel returns a contiguous one-dimensional array in the chosen traversal order. In F order the first index changes fastest.
numpy.ravel in practice
Reading two rows column first places 1 beside 4. Record the traversal convention as well as the shape so the receiving system can restore the layout.
import numpy as np
np.ravel([[1,2,3],[4,5,6]], order='F')Result [1, 4, 2, 5, 3, 6]
中文
按列读矩阵时,展平顺序是什么?
ravel 以指定遍历顺序返回一维连续数组。F 顺序让第一维的索引变化最快。
numpy.ravel 算例
两行数据按列读取时,第一列的 1 和 4 会相邻。若下游按行重建,必须同步记录遍历约定,不能只传形状。
import numpy as np
np.ravel([[1,2,3],[4,5,6]], order='F')结果 [1, 4, 2, 5, 3, 6]