Home > Net >  I want to convert a label numpy array into binary matrix?
I want to convert a label numpy array into binary matrix?

Time:06-20

I have numpy array:

a = [355, 355, 1005, 7005, 7005, 7005]

I like this to be converted into binary matrix without any loop (if possible using a single line of code) like this:

m = [[1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1]]

CodePudding user response:

The logic is not fully clear, do you want some kind of one-hot encoding?

a = np.array([355, 355, 1005, 7005, 7005, 7005])

m = (a==np.unique(a,)[:,None]).astype(int)

output:

array([[1, 1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 1]])
  • Related