Convert array of string (category) to array of int from a pandas dataframe

前端 未结 4 1602
孤城傲影
孤城傲影 2020-12-14 11:25

I am trying to do something very similar to that previous question but I get an error. I have a pandas dataframe containing features,label I need to do some convertion to se

4条回答
  •  一向
    一向 (楼主)
    2020-12-14 12:26

    because none of these work for dimensions>1, I made some code working for any numpy array dimensionality:

    def encode_categorical(array):
        d = {key: value for (key, value) in zip(np.unique(array), np.arange(len(u)))}
        shape = array.shape
        array = array.ravel()
        new_array = np.zeros(array.shape, dtype=np.int)
        for i in range(len(array)):
            new_array[i] = d[array[i]]
        return new_array.reshape(shape)
    

提交回复
热议问题