Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences of transpose functions in python

I am generating a large matrix (100x100, let's call it X) with random numbers, with numpy.matrix() so that I have a numpy.ndarray.

I have been wondering if there are any difference between the two operations:

  1. numpy.transpose(X)
  2. X.T

I have measured the time of each operation in a loop with a range of 1000 and it seems that X.T is significantly faster than numpy.transpose(X)

Added Benchmarks:

For a 100x100 matrix I got the following results with X.T and numpy.tranpose(X)

In a 10.000 range loop:

  • 7421/10.000: X.T fastest
  • 1256/10.000: numpy.transpose(X) fastest
  • 1323/10.000: Same computation time or difference too small to determine

Added the code below

    import numpy as np
    import time

    np_transpose_count = 0
    T_transpose_count = 0
    equal_count = 0

    for i in range(10000):
       Se = np.random.rand(100,100)

       tic1 =time.clock()
       ST_T = Se.T
       toc1=time.clock()

       tic2 =time.clock()
       ST_np = np.transpose(Se)
       toc2=time.clock()

       if (toc1-tic1) < (toc2-tic2):
           T_transpose_count+=1
       elif (toc1-tic1) > (toc2-tic2):
           np_transpose_count+=1
       else:
           equal_count+=1

    print(T_transpose_count, np_transpose_count, equal_count)

Best regards Whir

like image 916
Whir Avatar asked May 24 '16 08:05

Whir


People also ask

What is difference between .T and transpose () in NumPy?

T and the transpose() call both return the transpose of the array. In fact, . T return the transpose of the array, while transpose is a more general method_ that can be given axes ( transpose(*axes) , with defaults that make the call transpose() equivalent to . T ).

What is the function of transpose in Python?

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.

Is reshape same as transpose?

reshape where possible is just a view . It works with the same data-buffer, but with a new shape and strides . transpose is also a view, but with a different strides .

What does NumPy transpose do?

Reverse or permute the axes of an array; returns the modified array. For an array a with two axes, transpose(a) gives the matrix transpose.


1 Answers

Using Ipython %timeit magic I get:

In [218]: X=np.ones((100,100))

In [219]: timeit X.T
1000000 loops, best of 3: 379 ns per loop

In [220]: timeit X.transpose()
1000000 loops, best of 3: 470 ns per loop

In [221]: timeit np.transpose(X)
1000000 loops, best of 3: 993 ns per loop

In [222]: timeit X+1
10000 loops, best of 3: 21.6 µs per loop

So yes, .T is fastest, and the function slowest. But compare those times with the time for a simple addition

Or a copy or slice

In [223]: timeit X.copy()
100000 loops, best of 3: 10.8 µs per loop

In [224]: timeit X[:]
1000000 loops, best of 3: 465 ns per loop

Transpose in all its forms returns a new array object, with new shape and strides, but with a shared data buffer (Look at the .__array_interface__ dictionary to see that). So it takes about the same time as other actions that return a view. But none of the transpose functions makes a copy of the data or iterates through it. So the time differences are just the result of calling over head.

Again with ipython magic

np.transpose??
def transpose(a, axes=None):
    try:
        transpose = a.transpose
    except AttributeError:
        return _wrapit(a, 'transpose', axes)
    return transpose(axes)

So np.function(X) ends up calling X.transpose().

I'd have to look at the numpy code, but I recall that .T is implemented as an attribute (not quite the same as a property). I suspect it is faster because it doesn't use the axes parameter, and thus saves a C function call or two.

like image 129
hpaulj Avatar answered Oct 08 '22 19:10

hpaulj