Ich bin auf der Suche nach einem vektorisierten Weg, um eine numpy.array
von numpy.array
von Indizes zu indexieren.Index 2D numpy Array von einem 2D-Array von Indizes ohne Loops
Zum Beispiel:
import numpy as np
a = np.array([[0,3,4],
[5,6,0],
[0,1,9]])
inds = np.array([[0,1],
[1,2],
[0,2]])
Ich möchte ein neues Array aufzubauen, so dass jede Zeile (i) in diesem Array ist eine Zeile (i) der Array a
durch Zeile des Arrays inds indexiert (i). Meine gewünschte Ausgabe ist:
array([[ 0., 3.], # a[0][:,[0,1]]
[ 6., 0.], # a[1][:,[1,2]]
[ 0., 9.]]) # a[2][:,[0,2]]
ich dies mit einer Schleife erreichen kann:
def loop_way(my_array, my_indices):
new_array = np.empty(my_indices.shape)
for i in xrange(len(my_indices)):
new_array[i, :] = my_array[i][:, my_indices[i]]
return new_array
Aber ich bin auf der Suche nach einer reinen vektorisiert Lösung.