【问题标题】:One-Hot Encode Features With Multiple Labels具有多个标签的 One-Hot 编码功能
【发布时间】:2019-09-07 21:44:16
【问题描述】:

在 python 中,我们可以使用多个标签制作 One-Hot Encode 功能 示例:https://chrisalbon.com/machine_learning/preprocessing_structured_data/one-hot_encode_features_with_multiple_labels/

我有一个包含几列的数据框,最后一列是标签。

这个标签是一个这样的列表(每行一个新行):

Label
"A"
"B"
"C"
"D"
"A,B,C"
"A,C"
"D,B,A"
"D,C,B,A"

我试试:

levels(data_Frame$Label)<-c("A","B","C","D")
New_data_Frame<-as.data.frame(decodeClassLabels(data_Frame$Label))

但我得到的是:

A   B   C   D
1   0   0   0
0   1   0   0 
0   0   1   0 
0   0   0   1 
0   0   0   0 
0   0   0   0 
0   0   0   0
0   0   0   0 

我想要的是:

A   B   C   D
1   0   0   0
0   1   0   0 
0   0   1   0 
0   0   0   1 
1   1   1   0 
1   0   1   0 
1   1   0   1
1   1   1   1 

【问题讨论】:

  • 试试library(qdapTools);mtabulate(strsplit(df1$Labels, ","))

标签: r autoencoder one-hot-encoding


【解决方案1】:

一种选择是用, 拆分“标签”列,然后使用mtabulate

library(qdapTools)
+(mtabulate(strsplit(df1$Label, ",")) > 0) 
#     A B C D
#[1,] 1 0 0 0
#[2,] 0 1 0 0
#[3,] 0 0 1 0
#[4,] 0 0 0 1
#[5,] 1 1 1 0
#[6,] 1 0 1 0
#[7,] 1 1 0 1
#[8,] 1 1 1 1

数据

df1 <- structure(list(Label = c("A", "B", "C", "D", "A,B,C", "A,C", 
"D,B,A", "D,C,B,A")), class = "data.frame", row.names = c(NA, 
  -8L))

【讨论】:

  • Amazing 正在发挥作用,而且非常简单。在我的帖子解决问题 27 分钟后,我失去了一周的时间来寻求解决方案。
猜你喜欢
  • 2021-12-13
  • 2021-08-05
  • 2019-10-27
  • 2020-07-18
  • 2020-11-04
  • 1970-01-01
  • 2020-10-08
  • 2017-07-27
  • 1970-01-01
相关资源
最近更新 更多