2016-04-13 18 views
0

Ich versuche, eine Funktion zu schreiben, die in einer Liste von 3D-Matrices nimmt ..Konvertieren eine Liste der 3D-Matrix in 4D Matrix

so .. jedes Element in der Liste hat Form (rows,cols, some_scalar).. ich es in 4d Matrix neu zu gestalten versuchen .. so output = (number_of_elements_in_matrix, rows,cols,some_scalar)

bisher habe ich ist

output = np.zeros((len(list_of_matrices), list_of_matrices[0].shape[0], list_of_matrices[0].shape[1], 
         list_of_matrices[0].shape[2]), dtype=np.uint8) 

Wie weiß ich, diesen Ausgang 4d Tensor mit den Werten füllen ..

def reshape_matrix(list_of_matrices): 
    output = np.zeros((len(list_of_matrices), list_of_matrices[0].shape[0], list_of_matrices[0].shape[1], 
          list_of_matrices[0].shape[2]), dtype=np.uint8) 


    return output 
+0

@Divakar: ja .. das hat seinen Zweck erfüllt .., wenn Sie es als Antwort schreiben wollen? – Fraz

Antwort

1

Sie können np.stack verwenden, um entlang der ersten Achse (Achse = 0) zu stapeln, so -

np.stack(list_of_matrices,axis=0) 

Probelauf -

In [22]: # Create an input list of arrays 
    ...: arr1 = np.random.rand(4,5,2) 
    ...: arr2 = np.random.rand(4,5,2) 
    ...: arr3 = np.random.rand(4,5,2) 
    ...: list_of_matrices = [arr1,arr2,arr3] 
    ...: 

In [23]: np.stack(list_of_matrices,axis=0).shape 
Out[23]: (3, 4, 5, 2) 
Verwandte Themen