简体   繁体   English

我正在尝试对三组数据执行一次热编码

[英]I am trying to perform one hot encoding for a three sets of data

I have a list (length=228) that represents the label of the columns (ie column.head).Also, I have three samples (a,b,c), for each of these samples, I have to create one-hot encoding.我有一个列表(长度 = 228)代表列的标签(即 column.head)。此外,我有三个样本(a、b、c),对于这些样本中的每一个,我必须创建一个热编码。 How to do this.这该怎么做。 The result may look like a matrix with o and one values inside with dimension (3,228)结果可能看起来像一个矩阵,其中包含 o 和一个维度为 (3,228) 的值

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)

You create a list called matrice and you add sublists for each row with 1 if index matching 0 else您创建一个名为矩阵的列表,并为每行添加子列表,如果索引匹配 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)])

You can try:你可以试试:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM