首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算差异或比较两个字典- Groundtruth和clustering

计算差异或比较两个字典- Groundtruth和clustering
EN

Stack Overflow用户
提问于 2020-06-30 15:51:37
回答 1查看 65关注 0票数 1

我有两个字典h和c。这里1,2,3是文件夹名称和IMG_0001...是每个特定文件夹中包含的所有图像文件。

这是我的基本事实

代码语言:javascript
复制
h = {'1': ['IMG_0001.png', 'IMG_0002.png', 'IMG_0003.png', 'IMG_0004.png'], 
     '2': ['IMG_0020.png', 'IMG_0021.png', 'IMG_0022.png', 'IMG_0023.png'], 
     '3': ['IMG_0051.png', 'IMG_0052.png', 'IMG_0053.png', 'IMG_0054.png']} 

这是我的聚类输出图像

代码语言:javascript
复制
c = {'1': ['IMG_0001.png', 'IMG_0002.png', 'IMG_0053.png', 'IMG_0054.png'], 
     '2': ['IMG_0020.png', 'IMG_0021.png', 'IMG_0022.png', 'IMG_0023.png'], 
     '3': ['IMG_0003.png', 'IMG_0004.png', 'IMG_0051.png', 'IMG_0052.png']}

现在,我必须检查和比较两个字典,并为每个文件夹生成一个accuracy_score。如何用python编写代码。有一个聚类评估指标-调整后的随机数索引(ARI),但我不知道在这里我应该如何使用它来比较groundtruth和聚类字典。感谢你的帮助。非常感谢您的宝贵时间。我是python的初学者。

代码语言:javascript
复制
import os, pprint
pp = pprint.PrettyPrinter()
h={}
for subdir, dirs, files in os.walk(r"folder_paths"):    
    for file in files:
        key, value = os.path.basename(subdir), file  #Get basefolder name & file name
        h.setdefault(key, []).append(value)          #Form DICT
pp.pprint(h)


#####################################

import os, pprint
pp = pprint.PrettyPrinter()
c={}
for subdir, dirs, files in os.walk(r"folder_paths"):    
    for file in files:
        key, value = os.path.basename(subdir), file  #Get basefolder name & file name
        c.setdefault(key, []).append(value)          #Form DICT
pp.pprint(c)

#####################################


# diff = {}
# #value = set(h.values()).intersection(set(c.values()))
# value = { k : second_dict[k] for k in set(second_dict) - set(first_dict) }
# print(value)

print("Changes in Ground Truth and Clustering")
import dictdiffer
for diff in list(dictdiffer.diff(h, c)):         
    print(diff)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-30 16:29:38

代码语言:javascript
复制
from sklearn.metrics import accuracy_score

h = {'1': ['IMG_0001.png', 'IMG_0002.png', 'IMG_0003.png', 'IMG_0004.png'],
     '2': ['IMG_0020.png', 'IMG_0021.png', 'IMG_0022.png', 'IMG_0023.png'],
     '3': ['IMG_0051.png', 'IMG_0052.png', 'IMG_0053.png', 'IMG_0054.png']}

c = {'1': ['IMG_0001.png', 'IMG_0002.png', 'IMG_0053.png', 'IMG_0054.png'],
     '2': ['IMG_0020.png', 'IMG_0021.png', 'IMG_0022.png', 'IMG_0023.png'],
     '3': ['IMG_0003.png', 'IMG_0004.png', 'IMG_0051.png', 'IMG_0052.png']}

images = []
for key, value in h.items():
    images.extend(value)
print(images)  # ['IMG_0001.png', 'IMG_0002.png', 'IMG_0003.png', 'IMG_0004.png', 'IMG_0051.png', ..., 'IMG_0023.png']

reverse_h = {}
for key, value in h.items():
    for img in value:
        reverse_h[img] = key
print(reverse_h)  # {'IMG_0003.png': '1', 'IMG_0051.png': '3', 'IMG_0004.png': '1', ..., 'IMG_0054.png': '3'}

y_true = [reverse_h[img] for img in images]
print(y_true)  # ['1', '1', '1', '1', '3', '3', '3', '3', '2', '2', '2', '2']

reverse_c = {}
for key, value in c.items():
    for img in value:
        reverse_c[img] = key

print(reverse_c)  # {'IMG_0053.png': '1', 'IMG_0020.png': '2', 'IMG_0003.png': '3', ..., 'IMG_0054.png': '1'}

y_pred = [reverse_c[img] for img in images]
print(y_pred)  # ['1', '1', '3', '3', '3', '3', '1', '1', '2', '2', '2', '2']

score = accuracy_score(y_true, y_pred)
print(score)  # 0.6666666666666666
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62652691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档