I have a matrix, with many rows, and 8 columns. Each cell represents a probability for the current row to belong to 1 of the 8 classes. I would like to keep only the 2 highest values in each row, and set the rest to 0.
So far, the only way I can think of is by looping and sorting each row separately. For example:
a = np.array([[ 0.2 , 0.1 , 0.02 , 0.01 , 0.031, 0.11 ],
[ 0.5 , 0.1 , 0.02 , 0.01 , 0.031, 0.11 ],
[ 0.2 , 0.1 , 0.22 , 0.15 , 0.031, 0.11 ]])
I would like to get:
array([[ 0.2 , 0. , 0. , 0. , 0. , 0.11],
[ 0.5 , 0. , 0. , 0. , 0. , 0.11],
[ 0.2 , 0. , 0.22, 0. , 0. , 0. ]])
Thanks,