Created
August 9, 2017 01:59
-
-
Save yingminc/c720f705dc9f2fee21c001a4897ba1e7 to your computer and use it in GitHub Desktop.
general one-hot dictionary and encoding
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 onehot_dict(input): | |
onehotd = {k:v for v,k in enumerate(set(input))} | |
return onehotd | |
def onehot_encoder(input,onehotd): | |
dim = len(set(input)) | |
n = len(input) | |
output = np.zeros((n,dim)) | |
for ind, i in enumerate(input): | |
output[ind][onehotd[i]]=1 | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment