I'm trying to implement a numpy function that replaces the max in each row of a 2D array with 1, and all other numbers with zero:
>>> a = np.array([[0, 1],
... [2, 3],
... [4, 5],
... [6, 7],
... [9, 8]])
>>> b = some_function(a)
>>> b
[[0. 1.]
[0. 1.]
[0. 1.]
[0. 1.]
[1. 0.]]
What I've tried so far
def some_function(x):
a = np.zeros(x.shape)
a[:,np.argmax(x, axis=1)] = 1
return a
>>> b = some_function(a)
>>> b
[[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]]