Skip to content Skip to sidebar Skip to footer

Numpy Transpose Functions Speed And Use Cases

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 f

Solution 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:

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

Where _wrapfunc in turn is just:

def_wrapfunc(obj, method, *args, **kwds):
    try:
        returngetattr(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.)

Post a Comment for "Numpy Transpose Functions Speed And Use Cases"