Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax in Python (.T)

Tags:

python

numpy

In the help resource for the multivariate normal sampling function in SciPy, they give the following example:

x,y = np.random.multivariate_normal(mean,cov,5000).T 

My question is rather basic: what does the final .T actually do?

Thanks a lot, I know it is fairly simple, but it is hard to look in Google for ".T".

like image 721
Leon palafox Avatar asked Apr 21 '11 08:04

Leon palafox


People also ask

What does .T in Python do?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

What is difference between .T and transpose () in NumPy?

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.

What is XT in Python?

X.T means transpose of X.

What is matrix T in Python?

property property matrix. T. Returns the transpose of the matrix.


1 Answers

The .T accesses the attribute T of the object, which happens to be a NumPy array. The T attribute is the transpose of the array, see the documentation.

Apparently you are creating random coordinates in the plane. The output of multivariate_normal() might look like this:

>>> np.random.multivariate_normal([0, 0], [[1, 0], [0, 1]], 5)   array([[ 0.59589335,  0.97741328],        [-0.58597307,  0.56733234],        [-0.69164572,  0.17840394],        [-0.24992978, -2.57494471],        [ 0.38896689,  0.82221377]]) 

The transpose of this matrix is:

array([[ 0.59589335, -0.58597307, -0.69164572, -0.24992978,  0.38896689],        [ 0.97741328,  0.56733234,  0.17840394, -2.57494471,  0.82221377]]) 

which can be conveniently separated in x and y parts by sequence unpacking.

like image 105
Sven Marnach Avatar answered Oct 05 '22 23:10

Sven Marnach