Skip to content

Instantly share code, notes, and snippets.

@jjone36
Created January 21, 2019 01:32
Show Gist options
  • Save jjone36/973c3100402d00c62575df16041a78dc to your computer and use it in GitHub Desktop.
Save jjone36/973c3100402d00c62575df16041a78dc to your computer and use it in GitHub Desktop.
import numpy as np
# one-hot-encoding
categories = np.array(['shirt', 'dress', 'shoe'])
labels = ['shoe', 'shirt', 'shoe', 'shirt', 'dress', 'dress', 'dress']
ohe_labels = np.zeros([len(labels), len(categories)])
for m in range(len(labels)):
# find the location of the label
j = np.where(categories == labels[m])
# give 1 to that corresponding position
ohe_labels[m, j] = 1
print(ohe_labels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment