Skip to content

Instantly share code, notes, and snippets.

@coreyauger
Created May 9, 2018 21:30
Show Gist options
  • Save coreyauger/5be62b9854a88f1b7ae48fc0e9594a91 to your computer and use it in GitHub Desktop.
Save coreyauger/5be62b9854a88f1b7ae48fc0e9594a91 to your computer and use it in GitHub Desktop.
convert classes to one-hot
def toClasses(labels, num_classes):
sorted = np.sort(np.array(labels, copy=True))
bsize = math.floor( len(sorted) / num_classes )
buckets = []
for i in range(num_classes):
buckets.append(sorted[i*bsize])
print("buckets: " + str(buckets))
targets = np.digitize(labels, buckets) - 1
one_hot_targets = np.eye(num_classes)[targets]
print(one_hot_targets)
return one_hot_targets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment