So ordnen Sie ein 1D-numpy-Arrays zu 2D-numpy-array?

Betrachten Sie das folgende einfache Beispiel:

X = numpy.zeros([10, 4])  # 2D array
x = numpy.arange(0,10)    # 1D array 

X[:,0] = x # WORKS

X[:,0:1] = x # returns ERROR: 
# ValueError: could not broadcast input array from shape (10) into shape (10,1)

X[:,0:1] = (x.reshape(-1, 1)) # WORKS

Kann mir jemand erklären, warum numpy hat Vektoren der Form (N,) statt (N,1) ?
Was ist der beste Weg, um das Gießen von 1D-array in 2D-array?

Warum brauche ich diese?
Weil ich habe einen code welcher Ergebnis x in ein 2D-array X und die Größe von x ändert sich von Zeit zu Zeit, so habe ich X[:, idx1:idx2] = x die funktioniert, wenn x ist der 2D-auch, aber nicht wenn x 1D.

Schreibe einen Kommentar