Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dimshuffle equivalent function in Numpy

I often use theano.tensor.dimshuffle. Is there an equivalent function for Numpy?

I guess I could do the same via several numpy.swapaxes and numpy.newaxis (for broadcast dimensions), numpy.reshape but is there some simpler or more direct way, just like dimshuffle?

like image 924
Albert Avatar asked Oct 30 '15 12:10

Albert


People also ask

What does .T mean in Python?

The . T accesses the attribute T of the object, which happens to be a NumPy array. The T attribute is the transpose of the array, see the documentation.

What does NP transpose do?

The numpy. transpose() function changes the row elements into column elements and the column elements into row elements. The output of this function is a modified array of the original one.


1 Answers

The function numpy.transpose permits any permutation of the axes of an array.

The variety array.T is a special case of this, corresponding to array.transpose() without arguments, which defaults to array.transpose(range(array.ndim)[::-1]).

numpy.swapaxes is numpy.transpose restricted to permutations of two axes.

theano.tensor.dimshuffle essentially corresponds to numpy.transpose, but in addition, it permits the creation of new axes of length 1 for broadcasting, by adding 'x' wherever an axis should be created. In numpy, this can be achieved using a combination of transpose and reshape.

Note that in numpy, care is taken to make transpose return a view on the data whenever possible. In theano this is probably the case, too, but may depend on how the code is optimized.

like image 100
eickenberg Avatar answered Sep 23 '22 03:09

eickenberg