Created
August 3, 2017 02:56
-
-
Save abehmiel/3fbd3b3e2957c8c9d8d9c88aaedd1f16 to your computer and use it in GitHub Desktop.
One hot encode function (for model-building, keras, etc)
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
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