【问题标题】:When saving a list of LabelEncoders the classes_ get overwritten by the last LabelEncoder保存 LabelEncoder 列表时,classes_ 被最后一个 LabelEncoder 覆盖
【发布时间】:2022-08-12 16:58:09
【问题描述】:

我试图保存 LE 编码器的 dict 以用于推理,这是训练和应用 LE 的代码,然后将 LE 保存到 dict (label_object) 中,然后将是 joblib.dump(ed)()

for col in data:
    if data[col].dtype == \'object\':
        # If 2 or fewer unique categories
        if len(list(data[col].unique())) >= 2:
            # Train on the training data
            le.fit(data[col])
            label_object[col] = le
            # Transform both training and testing data
            data[col] = le.transform(data[col])
            label_object[col] = le

尝试此操作时,LE 的 classes_ 似乎被最后一个 LE 覆盖,在本例中为 \'day_of_incident\'

我不确定是什么导致了这个问题,是代码逻辑有问题还是我做错了什么?

    标签: python scikit-learn data-science


    【解决方案1】:

    如果您想遍历数据框中的列,则需要将第一行更改为:

    for col in data.columns:
    

    【讨论】:

    • 不,这是错误的,试试看。 [x for x in df] 返回列列表。
    猜你喜欢
    • 2019-07-03
    • 1970-01-01
    • 2014-09-06
    • 2020-08-18
    • 2013-10-03
    • 2017-07-26
    • 2019-03-23
    • 2021-02-05
    • 2016-11-24
    相关资源
    最近更新 更多