Home > Software design >  Map classes to Pandas one hot encoding
Map classes to Pandas one hot encoding

Time:11-03

Given the below sequence:

[I, Z, S, I, I, J, N, J, I]

and given the below Pandas data frame:

char  fricative  nasal  lateral  labial  coronal  dorsal  frontal
I             0      0        0       0        0       0        1
J             0      0        1       0        1       0        1
N             0      1        0       0        0       1        0
S             1      0        0       0        1       0        0
Z             1      0        0       0        1       0        0

How can I map each character from the sequence to it's respective one hot vector from the data frame?

CodePudding user response:

Use:

df = df.set_index("char")
res = df.loc[sequence, :].to_numpy().tolist()

Output

[[0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 0], [1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 1, 0, 1], [0, 1, 0, 0, 0, 1, 0], [0, 0, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1]]
  • Related