gpt4 book ai didi

python - 检查二维数组每行中不同列索引的值

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:05 24 4
gpt4 key购买 nike

我有一些二进制 2D numpy 数组(预测),例如:

[
[1 0 1 0 1 1],
[0 0 1 0 0 1],
[1 1 1 1 1 0],
[1 1 0 0 1 1],
]

二维数组中的每一行是一个句子作为某些类别的分类,二维数组中的每一列对应于该句子的一个类别的分类。例如,类别(categories 数组)为 ['A','B','C','D','E','F']

我有另一个二维数组 (catIndex),其中包含要在每行中检查的值的索引,例如

[[0],
[4],
[5],
[2]
]

对于上面的 4 个实例。

我现在想做的是循环遍历二进制二维数组,并针对为每个句子指定的列索引,检查它是 1 还是 0,然后将相应的类别追加到新数组中 (catResult = [])。如果它是 0,我会将 "no_region" 附加到新数组。

例如,在句子 1 中,我查看该句子的索引 0,并检查它是 0 还是 1 。它是 1,因此我将 'A' 附加到我的新数组中。在第 2 句中,我查看该句子的索引 4,发现它是 0,因此我将 "no_region" 附加到数组中.

当前代码:

for index in catIndex:
for i,sentence in enumerate(prediction):
for j,binaryLabel in enumerate(sentence):
if prediction[i][index]==1:
catResult.append(categories[index])
else:
catResult.append("no_region")

最佳答案

制作二维数组:

In [54]: M=[[1,0,1,0,1,1],[0,0,1,0,0,1],[1,1,1,1,1,0],[1,1,0,0,1,1]]
In [55]: M=np.array(M)

列索引为ind,行索引为[0,1,2,3]:

In [56]: ind=[0,4,5,2]    
In [57]: m=M[np.arange(len(ind)),ind]
In [58]: m
Out[58]: array([1, 0, 0, 0])

使用ind映射标签:

In [59]: lbl=np.array(list('ABCDEF'),dtype=object)    
In [60]: res=lbl[ind]
In [61]: res
Out[61]: array(['A', 'E', 'F', 'C'], dtype=object)

使用where来确定是否使用该映射值,或者使用None。使用 object dtype 可以轻松地将字符串标签替换为其他内容,例如 Noneno_region 等。

In [62]: np.where(m, res, None)
Out[62]: array(['A', None, None, None], dtype=object)

关于python - 检查二维数组每行中不同列索引的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com