Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

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

Writer Emily Wong

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]
2

2 Answers

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.

1

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy