Reputation: 41
What is the difference between ndarray.transpose
and numpy.transpose
? and in which scenario, we have to which one ? is there a scenario where only one of the above will work ?
I have gone through the documentation and per my understanding numpy.transpose
will return a view whenever possible. Whereas ndarray.transpose
returns a view always.
It would be great if someone could give me some example where only one of the above is a good fit.
Upvotes: 1
Views: 272
Reputation: 231510
The code for np.transpose
is:
def transpose(a, axes=None):
return _wrapfunc(a, 'transpose', axes)
which is, effectively:
np.asarray(a).transpose(axes)
that is, make a no-copy array and apply the method.
If a
is already an array, the two approaches are essentially the same. Either way transpose
is a low cost operation, just changing shape
and strides
attributes. Use which ever makes your code clearest (that is, readable to a human).
Upvotes: 1