【问题标题】:Encoding categorical numeric data to different columns将分类数值数据编码到不同的列
【发布时间】:2020-01-18 09:13:35
【问题描述】:

我是数据科学和机器学习的新手。我有一列包含三个值 0、1、2,我想将这 3 个值编码为 3 个不同的列,前缀为 predict_。

我尝试过 get_dummies 和标签编码器,但没有成功

import pandas as pd
Y = pd.get_dummies(Y,prefix='predict_')

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
Y = le.fit_transform(Y)

最后,列应该看起来像 predict_0,predict_1,predict_2

【问题讨论】:

  • 您可以使用pd.get_dummiesLabelEncoder,但不能同时使用。
  • Hi Quang...我正在使用其中一个,但没有得到任何积极的结果。
  • 我的 DF 看起来像:id 50078 non-null int64 location 50078 non-null object fault_severity 50078 non-null int64 log_feature 50078 non-null object volume 50078 non-null int64 event_type 50078 non-null object resource_type 50078 non-null object severity_type 50078 non-null object 我已分配 Y = A['fault_severity'] 并尝试使用其中一个但没有运气

标签: pandas scikit-learn data-science


【解决方案1】:

确保只传递列

pd.get_dummies(Y['column_name'],prefix='predict_')

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2020-12-21
    • 2020-09-11
    • 2019-09-14
    • 2020-03-21
    相关资源
    最近更新 更多