2016-03-31 18 views
-3

So habe ich ein Array in numpy, Python, die wie folgt aussieht:Ändern einen Array aus (Zeilen, Spalten) bis (Zeilen) in numpy

print array 
[[1, 2, 3, 4, 5, 6, 7]] 

aber ich möchte, dies ändern:

print array 
[1, 2, 3, 4, 5, 6, 7] 

Das ursprüngliche Array war:

print array 
[[ 1] 
[ 2] 
[ 3] 
[ 4] 
[ 5] 
[ 6] 
[ 7]] 

und I ch mit ANGED es meinen Array:

x = np.reshape(1, len(array)) 

Wie kann ich diese Änderung kompletten Einbau in numpy Funktionen?
Ich möchte keine Schleifen verwenden, da ich beim Durchlaufen großer Datenmengen Geschwindigkeit benötige.

+0

Bitte prüfen [Wie man einen guten fragen Frage] (http://stackoverflow.com/help/how-to-ask). Dies ist eine höfliche Art zu sagen, dass Sie ein Minimum an Aufwand investieren müssen, was hier nicht ersichtlich ist. –

+0

Fragen, die Debugging-Hilfe suchen ("** warum funktioniert dieser Code nicht? **") müssen das gewünschte Verhalten, ein ** spezifisches Problem ** mit der ** vollständigen Fehlermeldung und/oder Stacktrace ** und das * beinhalten * Der kürzeste Code ist notwendig ** um ihn ** in der Frage selbst zu reproduzieren **. Fragen ohne ** eine klare Problemstellung ** sind für andere Leser nicht nützlich. Siehe: [Erstellen eines minimalen, vollständigen und überprüfbaren Beispiels.] (Http://stackoverflow.com/help/mcve). –

+0

Ich habe den Verdacht, dass Sie diese Transformation nicht wirklich durchführen müssen. Was versuchst du eigentlich? –

Antwort

2

Sie np.flatten auf Ihrem Array verwenden können:

>>> x 
array([[1], 
     [2], 
     [3], 
     [4], 
     [5]]) 
>>> x.flatten() 
array([1, 2, 3, 4, 5]) 
0

np.reshape(x,[-1])

Von der Hilfe:

newshape : int or tuple of ints 
     The new shape should be compatible with the original shape. If 
     an integer, then the result will be a 1-D array of that length. 
     One shape dimension can be -1. In this case, the value is inferred 
     from the length of the array and remaining dimensions. 
    order : {'C', 'F', 'A'}, optional 
     Read the elements of `a` using this index order, and place the elements 
     into the reshaped array using this index order. 'C' means to 
     read/write the elements using C-like index order, with the last axis index 
     changing fastest, back to the first axis index changing slowest. 'F' 
     means to read/write the elements using Fortran-like index order, with 
     the first index changing fastest, and the last index changing slowest. 
     Note that the 'C' and 'F' options take no account of the memory layout 
     of the underlying array, and only refer to the order of indexing. 'A' 
     means to read/write the elements in Fortran-like index order if `a` is 
     Fortran *contiguous* in memory, C-like order otherwise. 
g=np.array([1, 2, 3, 4, 5]) 
d=np.reshape(g,(1,-1)) 
print(d) 
>>>[[1 2 3 4 5]] 
f=np.reshape(d,[-1]) 
print(f) 
>>>array([1, 2, 3, 4, 5])