Numpy transpose functions speed and use cases

后端 未结 1 1396
借酒劲吻你
借酒劲吻你 2020-12-22 07:54

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

b = np.arange(10)

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

#Transpose functi         


        
相关标签:
1条回答
  • 2020-12-22 07:55

    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.)

    0 讨论(0)
提交回复
热议问题