Skip to content

Instantly share code, notes, and snippets.

@frnsys
Created May 6, 2016 02:05
Show Gist options
  • Save frnsys/91a69f9f552cbeee7b565b3149f29e3e to your computer and use it in GitHub Desktop.
Save frnsys/91a69f9f552cbeee7b565b3149f29e3e to your computer and use it in GitHub Desktop.
take a 2d numpy array of category labels and turn it into a 3d one-hot numpy array
import numpy as np
# the 2d array of our samples,
# each component is a category label
a = np.array([[1,2,3],[4,5,6]])
# the 3d array that will be the one-hot representation
# a.max() + 1 is the number of labels we have
b = np.zeros((a.shape[0], a.shape[1], a.max() + 1))
# if you visualize this as a stack of layers,
# where each layer is a sample,
# this first index selects each layer separately
layer_idx = np.arange(a.shape[0]).reshape(a.shape[0], 1)
# this index selects each component separately
component_idx = np.tile(np.arange(a.shape[1]), (a.shape[0], 1))
# then we use `a` to select indices according to category label
b[layer_idx, component_idx, a] = 1
# voila!
print(b)
@deanhoperobertson
Copy link

How does one reverse this?

@abdallah1097
Copy link

This really helped me! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment