I know there are sub-optimal solutions out there, but I'm trying to optimise my code. So far, the shortest way I found is this:
import numpy as np
from sklearn.preprocessing import OrdinalEncoder
target = np.array(['dog', 'dog', 'cat', 'cat', 'cat', 'dog', 'dog', 'cat', 'cat'])
oe = OrdinalEncoder()
target = oe.fit_transform(target.reshape(-1, 1)).ravel()
target = np.eye(np.unique(target).shape[0])[np.array(target, dtype=np.int32)]
print(target)
[[0. 1.]
[0. 1.]
[1. 0.]
[1. 0.]
...
This is ugly code, and very long. Remove any part of it and it won't work. I'm looking for a simpler way, that won't involve calls to more than half a dozen functions from two different libraries.
targetproduced byoe