i want to assign an array that specific (row, col) with value 1
here is my code:
fl = np.zeros((5, 3))
labels = np.random.random_integers(0, 2, (5, 1))
for i in range(5):
fl[i, labels[i]] = 1
is there some shortcut for the process?
You can use labels array as a boolean array with fl.shape as shape. Try:
import numpy as np
fl = np.zeros((5, 3))
labels = np.random.random_integers(0, 1, fl.shape).astype(bool)
fl[labels] = 1
And here is how the array of boolean's in labels and result will look like:
>>> labels
array([[False, True, False],
[ True, True, False],
[False, True, True],
[ True, True, True],
[ True, False, False]], dtype=bool)
>>> fl
array([[ 0., 1., 0.],
[ 1., 1., 0.],
[ 0., 1., 1.],
[ 1., 1., 1.],
[ 1., 0., 0.]])