Skip to content

Instantly share code, notes, and snippets.

@konverner
Created March 1, 2022 05:33
Show Gist options
  • Save konverner/6d3b2254b68131c9a3a8b892bb4c8928 to your computer and use it in GitHub Desktop.
Save konverner/6d3b2254b68131c9a3a8b892bb4c8928 to your computer and use it in GitHub Desktop.
onehot_encode.py
def encode_onehot(labels: list):
labels = np.array(labels)
shape = (labels.size, labels.max()+1)
one_hot = np.zeros(shape)
rows = np.arange(labels.size)
one_hot[rows, labels] = 1
return one_hot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment