How to Change Order of Array Dimensions

How to change order of array dimensions

The function for doing that is aperm, from the base package. It is a generalization of the transpose t() function to multidimensional arrays. For your example, you would call it as follows:

new.data <- aperm(old.data, c(2,3,1))

Swapping the dimensions of a numpy array

Please note: Jaime's answer is better. NumPy provides np.transpose precisely for this purpose.


Or use np.einsum; this is perhaps a perversion of its intended purpose, but the syntax is quite nice:

In [195]: A = np.random.random((2,4,3,5))

In [196]: B = np.einsum('klij->ijkl', A)

In [197]: A.shape
Out[197]: (2, 4, 3, 5)

In [198]: B.shape
Out[198]: (3, 5, 2, 4)

In [199]: import itertools as IT
In [200]: all(B[k,l,i,j] == A[i,j,k,l] for i,j,k,l in IT.product(*map(range, A.shape)))
Out[200]: True

Rearranging axes in numpy?

There are two options: np.moveaxis and np.transpose.

  • np.moveaxis(a, sources, destinations) docs

    This function can be used to rearrange specific dimensions of an
    array. For example, to move the 4th dimension to be the 1st and the 2nd dimension to be the last:

    >>> rearranged_arr = np.moveaxis(arr, [3, 1], [0, 3])
    >>> rearranged_arr.shape
    (40, 10, 30, 20)

    This can be particularly useful if you have many dimensions and only want to rearrange a small number of them. e.g.

    >>> another_arr = np.random.rand(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    >>> np.moveaxis(another_arr, [8, 9], [0, 1]).shape
    (8, 9, 0, 1, 2, 3, 4, 5, 6, 7)
  • np.transpose(a, axes=None) docs

    This function can be used to rearrange all dimensions of an array at once. For example, to solve your particular case:

    >>> rearranged_arr = np.transpose(arr, axes=[3, 0, 2, 1])
    >>> rearranged_arr.shape
    (40, 10, 30, 20)

    or equivalently

    >>> rearranged_arr = arr.transpose(3, 0, 2, 1)
    >>> rearranged_arr.shape
    (40, 10, 30, 20)

how to change a dimension of array in python?

you can reshape it using np.array()

b = np.zeros((192,192,1))
print(b.shape) #(192, 192, 1)
print(np.array([b]).shape) #(1, 192, 192, 1)

Efficiently change order of numpy array

You could reshape to split the first axis into two axes, such that latter of those axes is of length 2 and then flip the array along that axis with [::-1] and finally reshape back to original shape.

Thus, we would have an implementation like so -

a.reshape(-1,2,*a.shape[1:])[:,::-1].reshape(a.shape)

Sample run -

In [170]: a = np.random.randint(0,9,(6,3))

In [171]: order = [1,0,3,2,5,4]

In [172]: a[order]
Out[172]:
array([[0, 8, 5],
[4, 5, 6],
[0, 0, 2],
[7, 3, 8],
[1, 6, 3],
[2, 4, 4]])

In [173]: a.reshape(-1,2,*a.shape[1:])[:,::-1].reshape(a.shape)
Out[173]:
array([[0, 8, 5],
[4, 5, 6],
[0, 0, 2],
[7, 3, 8],
[1, 6, 3],
[2, 4, 4]])

Alternatively, if you are looking to efficiently create those constantly flipping indices order, we could do something like this -

order = np.arange(data.shape[0]).reshape(-1,2)[:,::-1].ravel()


Related Topics



Leave a reply



Submit