我有两个字典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']} 这是我的聚类输出图像
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的初学者。
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)发布于 2020-06-30 16:29:38
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.6666666666666666https://stackoverflow.com/questions/62652691
复制相似问题