我试图对三组数据执行一个热编码

2024-04-28 20:01:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表(长度=228),它表示列(即column.head)的标签。另外,我有三个样本(a、b、c),对于每个样本,我必须创建一个热编码。如何做到这一点。结果可能看起来像一个带有o的矩阵,其中一个值带有维度(3228)

a='95', '66', '137', '70', '20'
b='36', '66', '44', '214', '105', '133'
c='170', '66', '97', '153', '105', '138'
lnew=list(range(1,229))
lnew=list(map(str, total_labels))
print(lnew)

Tags: map编码列表labelsrangecolumn矩阵标签
2条回答

创建一个名为matrice的列表,如果索引与0匹配,则为每一行添加子列表(如果索引与0匹配,则为1)

a=[95, 66, 137, 70, 20]
b=[36, 66, 44, 214, 105, 133]
c=[170, 66, 97, 153, 105, 138]

matrice=[]
matrice.append([1 if i in a else 0 for i in range(229)])
matrice.append([1 if i in b else 0 for i in range(229)])
matrice.append([1 if i in c else 0 for i in range(229)])

您可以尝试:

a=[95, 66, 137, 70, 20]
b=[36, 66, 44, 214, 105, 133]
c=[170, 66, 97, 153, 105, 138]

df = pd.DataFrame()
count=0
for j in [a,b,c]:
    col = [1 if i in j else 0 for i in range(229)]
    df[count] = col
    count+=1
mat = np.matrix(df).reshape(3,228)

相关问题 更多 >