0
votes

Python remodèle un tableau 1D en un tableau 2D par `` lignes '' ou `` colonnes ''

Je voudrais contrôler comment array.reshape () remplit le nouveau tableau. Par exemple

a = np.arange(9).reshape(3,3, 'columnwise')
## array([[ 0,  3,  6,  9],
##   [ 1,  4,  7,  10],
##   [ 2,  5, 8, 11]]) 

mais ce que j'aimerais pouvoir, c'est remplir le tableau par colonne avec quelque chose comme:

a = np.arange(12).reshape(3,4)
## array([[ 0,  1,  2,  3],
##   [ 4,  5,  6,  7],
##   [ 8,  9, 10, 11]]) 


0 commentaires

3 Réponses :


2
votes

Utilisez np.transpose.

[[0 3 6]
 [1 4 7]
 [2 5 8]]

Output:

import numpy as np

print(np.arange(9).reshape(3,3).transpose())


0 commentaires

0
votes

Si vous transposez la matrice d'origine, vous obtiendrez l'effet souhaité.

a = np.arange(6).reshape(3,3).T

OU

import numpy as np
a = np.arange(6).reshape(3,3).tranpose()


0 commentaires

0
votes
In [22]:  np.arange(12).reshape(3,4, order='F')                                                        
Out[22]: 
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

0 commentaires