-3

I have some code here:

import numpy as np 
import matplotlib.pyplot as plt 

#height (cm)
X = np.array([[147, 150, 153, 158, 163, 165, 168, 170, 173, 175, 178, 180, 183]])
print(X.T)
print("=======================")
print(X)

Can anyone explain me what is T mean and the differences between X and X.T?

Wasif
  • 14,755
  • 3
  • 14
  • 34
Duy Long
  • 17
  • 1
  • 4

3 Answers3

1

The .T is an attribute of numpy array, that transposes the array.

Wasif
  • 14,755
  • 3
  • 14
  • 34
0

The significant differences are seen in the shape and strides attributes:

In [64]: X = np.array([[1,2,3,4]])
In [65]: X
Out[65]: array([[1, 2, 3, 4]])
In [66]: X.T
Out[66]: 
array([[1],
       [2],
       [3],
       [4]])
In [67]: X.shape
Out[67]: (1, 4)
In [68]: X.T.shape
Out[68]: (4, 1)
In [69]: X.strides
Out[69]: (32, 8)
In [70]: X.T.strides
Out[70]: (8, 32)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • So what is the conclusion? – Leonard Oct 27 '20 at 06:12
  • I don't think there needs to be a "conclutions". Everyone else is just calling it the transpose. But my numbers show what actually happens in `numpy` - it makes a `view` with different shape and strides. – hpaulj Oct 27 '20 at 15:53
0

The .T attribute returns the array transpose. Note that in your case, you declared almost a vector (a real vector would be one dimensional, but in your case you have two dimensions because of the double brackets [[]] use in the definition of x):

import numpy as np
x = np.array([[147, 150, 153, 158, 163, 165, 168, 170, 173, 175, 178, 180]])
print("Line vector:")
print(x)
print("Column vector:")
print(x.T)
Line vector:
[[147 150 153 158 163 165 168 170 173 175 178 180]]
Column vector:
[[147]
 [150]
 [153]
 [158]
 [163]
 [165]
 [168]
 [170]
 [173]
 [175]
 [178]
 [180]]

Note that in this case, there is no need to use double square brackets [[]]. If you define x with single brackets, the transpose is not different (because there is no dimension to transpose with):

import numpy as np
x = np.array([147, 150, 153, 158, 163, 165, 168, 170, 173, 175, 178, 180])
print("Line vector:")
print(x)
print("Column vector:")
print(x.T)
Line vector:
[147 150 153 158 163 165 168 170 173 175 178 180]
Column vector:
[147 150 153 158 163 165 168 170 173 175 178 180]
Leonard
  • 2,510
  • 18
  • 37