How to reverse sklearn.OneHotEncoder transform to recover original data?

后端 未结 8 1846
深忆病人
深忆病人 2020-12-13 07:34

I encoded my categorical data using sklearn.OneHotEncoder and fed them to a random forest classifier. Everything seems to work and I got my predicted output bac

8条回答
  •  温柔的废话
    2020-12-13 08:36

    How to one-hot encode

    See https://stackoverflow.com/a/42874726/562769

    import numpy as np
    nb_classes = 6
    data = [[2, 3, 4, 0]]
    
    def indices_to_one_hot(data, nb_classes):
        """Convert an iterable of indices to one-hot encoded labels."""
        targets = np.array(data).reshape(-1)
        return np.eye(nb_classes)[targets]
    

    How to reverse

    def one_hot_to_indices(data):
        indices = []
        for el in data:
            indices.append(list(el).index(1))
        return indices
    
    
    hot = indices_to_one_hot(orig_data, nb_classes)
    indices = one_hot_to_indices(hot)
    
    print(orig_data)
    print(indices)
    

    gives:

    [[2, 3, 4, 0]]
    [2, 3, 4, 0]
    

提交回复
热议问题