python - how to assign a value to numpy array in specific rows and cols -


i want assign array specific (row, col) value 1

here code:

fl = np.zeros((5, 3)) labels = np.random.random_integers(0, 2, (5, 1)) in range(5):     fl[i, labels[i]] = 1 

is there shortcut process?

here way it:

import numpy np fl = np.zeros((5, 3)) labels = np.random.random_integers(0, 2, 5) fl[range(0, 5), labels] = 1 

and produce output:

enter image description here


Comments