python - Array of 1's in indexed positions -
i have array of indices of minimum values in array.
it looks this:
[[0], [1], [2], [1], [0]]
(the maximum index 3)
what want array looks this:
[[1, 0, 0] [0, 1, 0] [0, 0, 1] [0, 1, 0] [1, 0, 0]]
where 1 in column of minimum.
is there easy way in numpy?
use numpy's broadcasting of ==
:
>>> minima = np.array([[0], [1], [2], [1], [0]]) >>> minima == arange(minima.max() + 1) array([[ true, false, false], [false, true, false], [false, false, true], [false, true, false], [ true, false, false]], dtype=bool) >>> (minima == arange(minima.max() + 1)).astype(int) array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]])
Comments
Post a Comment