Skip to content

Instantly share code, notes, and snippets.

@abehmiel
Created August 3, 2017 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abehmiel/3fbd3b3e2957c8c9d8d9c88aaedd1f16 to your computer and use it in GitHub Desktop.
Save abehmiel/3fbd3b3e2957c8c9d8d9c88aaedd1f16 to your computer and use it in GitHub Desktop.
One hot encode function (for model-building, keras, etc)
import numpy as np
def one_hot_encode_object_array(arr):
'''One hot encode a numpy array of objects (e.g. strings)'''
uniques, ids = np.unique(arr, return_inverse=True)
return np_utils.to_categorical(ids, len(uniques))
train_y_ohe = one_hot_encode_object_array(train_y)
test_y_ohe = one_hot_encode_object_array(test_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment