Numpy transpose functions speed and use cases

浪子不回头ぞ 提交于 2019-11-28 12:06:57

问题


So why is the NumPy transpose .T faster than np.transpose()?

b = np.arange(10)

#Transpose .T
t=b.reshape(2,5).T

#Transpose function
t = np.transpose(b.reshape(2,5))

#Transpose function without wrapper
t = b.reshape(2,5).transpose()

I did a timeit of both in Jupyter:

%timeit -n 1000 b.reshape(2,5).T

1000 loops, best of 3: 391 ns per loop

%timeit -n 1000 np.transpose(b.reshape(2,5))

1000 loops, best of 3: 600 ns per loop

%timeit -n 1000 b.reshape(2,5).transpose()

1000 loops, best of 3: 422 ns per loop

and to check scaleablility I did a larger matrix:

b = np.arange( 100000000)

%timeit -n 1000 b.reshape(10000,10000).T

1000 loops, best of 3: 390 ns per loop

%timeit -n 1000 np.transpose(b.reshape(10000,10000))

1000 loops, best of 3: 611 ns per loop

%timeit -n 1000 b.reshape(10000,10000).transpose()

1000 loops, best of 3: 435 ns per loop

In both cases the .T method about 2x faster than the wrapper and a bit faster than using .transpose() why is this? Is there a use case where np.transpose would be better?


回答1:


One reason might be that np.transpose(a) just calls a.transpose() internally, while a.transpose() is more direct. In the source you have:

def transpose(a, axes=None):
    return _wrapfunc(a, 'transpose', axes)

Where _wrapfunc in turn is just:

def _wrapfunc(obj, method, *args, **kwds):
    try:
        return getattr(obj, method)(*args, **kwds)
    except (AttributeError, TypeError):
        return _wrapit(obj, method, *args, **kwds)

This maps to getattr(a, 'transpose') in this case. _wrapfunc is used by many of the module-level functions to access methods, usually of the ndarray class or whatever is the class of the first arg.

(Note: .T is the same as .transpose(), except that the array is returned if it has <2 dimensions.)



来源:https://stackoverflow.com/questions/48509674/numpy-transpose-functions-speed-and-use-cases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!