【问题标题】:OneHotEncoding a (categorical) column but with the value of another column of the Datagrame (not 1)一个热编码一个(分类)列,但具有 Dataframe 另一列的值(不是 1)
【发布时间】:2023-02-23 01:11:30
【问题描述】:

(我在 StackOverFlow 上的第一个问题,所以请宽容)。

我正在对一组数据编写 ANN,其中包含以下列:

[... , 'labels_column', 'Content %']

我希望将 labels_column 编码为数字(就像我现在使用的 OneHotEncoder 一样),但希望值是来自列 'Content %' 的值,而不是 1

例如:

labels_column Content %
label_1 37
label_2 24
label_3 12
label_2 60

Transform后变成:

label_1 label_2 label_3
37 0 0
0 24 0
0 0 12
0 60 0

并不是: |标签_1 |标签_2 |标签_3 |含量 % | | ------ | ------ | ------ | ------ | | 1 | 0 | 0 | 37 | | 0 | 1 | 0 | 24 | | 0 | 0 | 1 | 12 | | 0 | 1 | 0 | 60 |

还没有设法用面具或其他技巧做到这一点......

非常感谢你的帮助!

【问题讨论】:

    标签: python transform one-hot-encoding


    【解决方案1】:

    你可以做一个数学/广播技巧:

    df = pd.DataFrame({'labels_column': ['label_1','label_2','label_3','label_2'],
                       'Content %': [37, 24, 12, 60]})
    
    pd.get_dummies(df['labels_column']) * df[['Content %']].values
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 2019-08-09
      • 2021-01-06
      • 2019-05-31
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多