>>> # Typically, transposition will change the order for
>>> # efficiency reasons: no need to change the data !
>>> n = np.random.random((2, 2, 2)).transpose()
>>> n.flags.f_contiguous
True
>>> n.flags.c_contiguous
False
>>> x, y, z = np.unravel_index([1,2,3,7], (2, 2, 2), order='F')
Numpy有一个函数
unravel_index
,它的作用相当于:给定一组“平面”索引,它将返回每个维度中索引数组的元组:然后可以
zip
它们来获得原始索引。你知道吗但是要注意,矩阵可以表示为“行序列”(C约定,
'C'
)或“列序列”(Fortran约定,'F'
),或更高维度的相应约定。numpy中典型的矩阵展平将保持这种顺序,因此[[1, 2], [3, 4]]
可以展平为[1, 2, 3, 4]
(如果它有'C'顺序)或[1, 3, 2, 4]
(如果它有'F'顺序)。unravel_index
如果要更改默认值(即“C”),则接受可选的order
参数,因此可以执行以下操作:相关问题 更多 >
编程相关推荐