Created
May 9, 2018 21:30
-
-
Save coreyauger/5be62b9854a88f1b7ae48fc0e9594a91 to your computer and use it in GitHub Desktop.
convert classes to one-hot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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