首页 > 解决方案 > 通过List在python中包含相等值的索引问题

问题描述

我有两个列表predicted_labels_ssup0predicted_labels_ssup1其中包含每个实例的 SVM 预测概率。不知何故,许多概率值是完全相等的。我的目标是提取最高概率值的索引并将其保存在另一个名为confident_inst_indexes.

所以这就是我的做法:

confident_inst_indexes=[]
for x,y in zip(predicted_labels_ssup0,predicted_labels_ssup1):


    if (x > 0.997):
        #print('x',x)

        #print(predicted_labels_ssup0.tolist().index(x))
        confident_inst_indexes= np.append(confident_inst_indexes,predicted_labels_ssup0.tolist().index(x))
    elif (y > 0.995):
        #print('y',y)
        #print(predicted_labels_ssup1.tolist().index(y))
        confident_inst_indexes= np.append(confident_inst_indexes,predicted_labels_ssup1.tolist().index(y))

这里的问题是,相等且满足固定条件的概率值总是得到相同的列表索引,即第一个占用该值的索引。

我怎样才能解决这个问题 ?有什么提议吗?

谢谢

标签: pythonlistkerassvm

解决方案


如果我很好地理解了您要做什么,您可以直接使用 numpy 库中的 argwhere 。它返回满足条件的所有索引

a = np.argwhere(predicted_labels_ssup0 > 0.997)
b = np.argwhere(predicted_labels_ssup1 > 0.995)
confident_inst_indexes = [a, b] # you may have to flatten too

我希望它有所帮助,

尼古拉斯


推荐阅读