Amb l'ajuda de Numpy numpy.transpose() , Podem realitzar la funció simple de transposar dins d'una línia utilitzant numpy.transpose() mètode de Numpy. Pot transposar les matrius 2-D, d'altra banda, no té cap efecte sobre les matrius 1-D. Aquest mètode transposa la matriu numpy 2D.
Paràmetres:
eixos: [Cap, tupla d'ints o n int] Si algú vol passar el paràmetre, ho podeu fer, però no és obligatori. Però si vols, recorda només passa (0, 1) o (1, 0) . Igual que tenim una matriu de forma (2, 3) per canviar-la (3, 2), hauríeu de passar (1, 0) on 1 com 3 i 0 com 2.
Devolucions: ndarray
Exemple #1:
En aquest exemple podem veure que és molt fàcil transposar una matriu amb només una línia.
Python 3
# importing python module named numpy> import> numpy as np> # making a 3x3 array> gfg> => np.array([[> 1> ,> 2> ,> 3> ],> > [> 4> ,> 5> ,> 6> ],> > [> 7> ,> 8> ,> 9> ]])> # before transpose> print> (gfg, end> => '
'> )> # after transpose> print> (gfg.transpose())> |
caràcter d'escapament java
>
>
inicialització de primaveraSortida:
[[1 2 3] [4 5 6] [7 8 9]] [[1 4 7] [2 5 8] [3 6 9]]>
Exemple #2:
En aquest exemple demostrem l'ús de tuples a numpy.transpose().
Python 3
# importing python module named numpy> import> numpy as np> # making a 3x3 array> gfg> => np.array([[> 1> ,> 2> ],> > [> 4> ,> 5> ],> > [> 7> ,> 8> ]])> # before transpose> print> (gfg, end> => '
'> )> # after transpose> print> (gfg.transpose(> 1> ,> 0> ))> |
>
>Sortida:
[[1 2] [4 5] [7 8]] [[1 4 7] [2 5 8]]>
Mètode 2: Utilitzant Objecte Numpy ndarray.T.
quina mida del meu monitor
Python 3
# importing python module named numpy> import> numpy as np> > # making a 3x3 array> gfg> => np.array([[> 1> ,> 2> ,> 3> ],> > [> 4> ,> 5> ,> 6> ],> > [> 7> ,> 8> ,> 9> ]])> > # before transpose> print> (gfg, end> => '
'> )> > # after transpose> print> (gfg.T)> |
java ha a continuació
>
>
Sortida
[[1 2 3] [4 5 6] [7 8 9]] [[1 4 7] [2 5 8] [3 6 9]]>