I got the following small numpy matrix, the values of the matrix can only be 0 or 1. The size of the actual matrix i am using is actually much bigger but for demonstration purposes this one is ok. The shape of it is (8, 11)
np_array = np.matrix(
[[0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,1,0,1,0,0,0,0,0],
[0,0,0,1,0,1,0,0,0,0,0],
[0,0,1,0,0,1,1,0,0,0,0],
[0,0,1,0,0,0,1,0,0,0,0],
[0,1,0,0,0,0,1,1,0,1,1],
[0,1,0,0,0,0,0,1,0,1,0],
[1,0,0,0,0,0,0,1,1,1,0]]
)
I need to change it in such a manner so that for each column there should be only a single row with the value of 1. So that if there is more rows with value of 1 for the same column the highest row with value of 1 is kept and the rest replaced with 0. Here is the result i am after:
np_array1 = np.matrix(
[[0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,1,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,1,0,1,1],
[0,0,0,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,0,0,1,0,0]]
)
Basically each column can have a single value of 1, if there are more than one rows, then keep the highest one. I must mention that there can be also columns where none of the rows have value 1. Those columns must be left unchanged. The shape of the matrix must be exactly as it was before the transformation.