Label encoding across multiple columns in scikit-learn

后端 未结 22 2441
礼貌的吻别
礼貌的吻别 2020-11-22 09:02

I\'m trying to use scikit-learn\'s LabelEncoder to encode a pandas DataFrame of string labels. As the dataframe has many (50+) columns, I want to a

22条回答
  •  感动是毒
    2020-11-22 09:27

    How about this?

    def MultiColumnLabelEncode(choice, columns, X):
        LabelEncoders = []
        if choice == 'encode':
            for i in enumerate(columns):
                LabelEncoders.append(LabelEncoder())
            i=0    
            for cols in columns:
                X[:, cols] = LabelEncoders[i].fit_transform(X[:, cols])
                i += 1
        elif choice == 'decode': 
            for cols in columns:
                X[:, cols] = LabelEncoders[i].inverse_transform(X[:, cols])
                i += 1
        else:
            print('Please select correct parameter "choice". Available parameters: encode/decode')
    

    It is not the most efficient, however it works and it is super simple.

提交回复
热议问题