def to_one_hot ( dataY ) : nc = 1 + np . max ( dataY ) onehot = [ np . zeros ( nc , dtype = np . int8 ) for _ in dataY ] for i , j in enumerate ( dataY ) : onehot [ i ] [ j ] = 1 return onehot
python onehotencoding for categorical variables
Convert the vector of labels dataY into one - hot encoding .
def to_one_hot(dataY):
    """Convert the vector of labels dataY into one-hot encoding.

    :param dataY: vector of labels
    :return: one-hot encoded labels
    """
    nc = 1 + np.max(dataY)
    onehot = [np.zeros(nc, dtype=np.int8) for _ in dataY]
    for i, j in enumerate(dataY):
        onehot[i][j] = 1
    return onehot