python - 通过List在python中包含相等值的索引问题
问题描述
我有两个列表predicted_labels_ssup0
,predicted_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))
这里的问题是,相等且满足固定条件的概率值总是得到相同的列表索引,即第一个占用该值的索引。
我怎样才能解决这个问题 ?有什么提议吗?
谢谢
解决方案
如果我很好地理解了您要做什么,您可以直接使用 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
我希望它有所帮助,
尼古拉斯
推荐阅读
- pagination - 如何在 v-data-table vuetify 中查看分页方向
- php - 这个for循环是什么意思?
- javascript - 如何使用 Flutter Web 配置网站不缓存数据?
- reactjs - 仅在显示道具时出现 Vercel 部署错误(在本地工作)
- javascript - 在 React JS 组件中使用 const 预期的表达式
- node.js - nodemon 没有在 webpack-typescript-node.js 中监视目录?
- java - 测试超时不适用于挂起的随机数生成器
- video - 播放两个视频,如何在 FFmpeg 中裁剪第二个视频
- python - 同时运行相同的 Databricks Python Notebook
- python - 合并两个 df 没有任何键