lotrpy
lotrpy

Reputation: 443

What's the difference of numpy.ndarray.T and numpy.ndarray.transpose() when self.ndim < 2

The document numpy.ndarray.T says

ndarray.T — Same as self.transpose(), except that self is returned if self.ndim < 2.

Also, ndarray.transpose(*axes) says

For a 1-D array, this has no effect.

Doesn't this mean the same thing?

Here's a little demo snippet:

>>> import numpy as np
>>> print np.__version__
1.5.1rc1
>>> a = np.arange(7)
>>> print a, a.T, a.transpose()
[0 1 2 3 4 5 6] [0 1 2 3 4 5 6] [0 1 2 3 4 5 6]

Upvotes: 32

Views: 24726

Answers (2)

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94565

It looks like .T is just a convenient notation, and that .transpose(*axes) is the more general function and is intended to give more flexibility, as axes can be specified. They are apparently not implemented in Python, so one would have to look into C code to check this.

Upvotes: 10

Matthew Rankin
Matthew Rankin

Reputation: 461167

Regardless of rank, the .T attribute and the .transpose() method are the same—they both return the transpose of the array.

In the case of a rank 1 array, the .T and .transpose() don't do anything—they both return the array.

Upvotes: 30

Related Questions