from keras.utils import np_utils uniques, ids = np.unique(arr, return_inverse=True) coded_array = np_utils.to_categorical(ids, len(uniques)) encode_dict ={} for i,j in zip(arr,coded_array): encode_dict[i] = j if len(encode_dict)==len(np.unique(arr)): break return coded_array,encode_dict
Example
Matching dictionary
{{'HOME': array([ 0., 0., 1.]), 'DRAW': array([ 0., 1., 0.]), 'AWAY': array([ 1., 0., 0.])}
Enter
['DRAW' 'HOME' 'HOME' ..., 'HOME' 'HOME' 'AWAY']
Coded output
[[ 0. 1. 0.] [ 0. 0. 1.] [ 0. 0. 1.] ..., [ 0. 0. 1.] [ 0. 0. 1.] [ 1. 0. 0.]]
How to cancel this function and get the decoding function?
source share