Skip to content

Instantly share code, notes, and snippets.

@okdolly-001
Created April 23, 2018 04:10
Show Gist options
  • Select an option

  • Save okdolly-001/c257d750ca72c69b1e416bbeba2adf51 to your computer and use it in GitHub Desktop.

Select an option

Save okdolly-001/c257d750ca72c69b1e416bbeba2adf51 to your computer and use it in GitHub Desktop.
One-hot encoding #numpy
def get_one_hot(targets, nb_classes):
return np.eye(nb_classes)[np.array(targets).reshape(-1)]
This is what happens:
np.eye(3)
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
np.eye(3)[2]
array([ 0., 0., 1.])
target = np.array([1,2])
np.eye(3)[target]
array([ 0., 1., 0.],[ 0., 0., 1.])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment