Basically, Numpy stores arrays as flat vectors. Multiple dimensions are just an illusion created by the different views and steps that the Numpy iterator uses.
For a detailed but easy explanation of how Numpy works, see the excellent chapter 19, Copy of the Beatiful Book .
At least Numpy array()
and reshape()
have an argument for C ('C'), Fortran ('F') or stored order ('A'). Also see the Question How to force the order of a numpy array for fortran style?
Example with default indexing C ( row order):
>>> a = np.arange(12).reshape(3,4)
>>> a = np.arange(12).reshape(3,4, order='F') >>> a array([[ 0, 3, 6, 9], [ 1, 4, 7, 10], [ 2, 5, 8, 11]]) >>> a[1] array([ 1, 4, 7, 10]) >>> a.strides (8, 24)
Another kind
In addition, you can always get a different view using the array parameter T:
>>> a = np.arange(12).reshape(3,4, order='C') >>> aT array([[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]]) >>> a = np.arange(12).reshape(3,4, order='F') >>> aT array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]])
You can also manually set the steps:
>>> a = np.arange(12).reshape(3,4, order='C') >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> a.strides (32, 8) >>> a.strides = (8, 24) >>> a array([[ 0, 3, 6, 9], [ 1, 4, 7, 10], [ 2, 5, 8, 11]])