[英]Calculate difference or compare two dictionaries - Groundtruth and clustering
我有两个字典 h 和 c。 这里 1,2,3 是文件夹名称,IMG_0001... 是每个特定文件夹中包含的所有图像文件。
这是我的基本事实
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']}
这是我的聚类 output 图像
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 中编写代码。 有一个集群评估指标 - Adjusted Rand Index (ARI),但不知道我应该如何在这里使用它来比较 groundtruth 和集群字典。 感谢你的帮助。 非常感谢您的参与。 我是 python 的初学者。
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)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.