[英]Python , how do I get the percentage accuracy between 2 list of lists
Supoose I have 2 matrices (list of lists)假设我有 2 个矩阵(列表列表)
list1 = [[1, 2, 3, 4], [2, 6, 7, 7], [3, 6, 2, 9], [4, 7, 4, 3]]
list2 = [[1, 2, 3, 4], [9, 4, 3, 5], [3, 5, 2, 7], [1, 9, 8, 3]]
How would I get the percentage accuracy between the 2. So if both matrices have exactly the same values (in exactly the same lists) then the accuracy is 100%我将如何获得 2 之间的百分比准确度。因此,如果两个矩阵具有完全相同的值(在完全相同的列表中),那么准确度为 100%
I have already tried using我已经尝试过使用
len(set(test_list1) & set(test_list2)) / float(len(set(test_list1) | set(test_list2))) * 100
but it only works for singular lists not list of lists但它只适用于单数列表而不是列表列表
This works fine这工作正常
list1 = [[1, 2, 3, 4], [2, 6, 7, 7], [3, 6, 2, 9], [4, 7, 4, 3]]
list2 = [[1, 2, 3, 4], [2, 6, 7, 7], [3, 6, 2, 9], [4, 7, 4, 3]]
first_list = [item for sublist in list1 for item in sublist]
second_list = [item for sublist in list2 for item in sublist]
common = set(first_list).intersection(second_list)
total = set(first_list).union(second_list)
print((len(common)/len(total))*100)
Hop it helps:跳它有帮助:
result = []
for test_list1, test_list2 in zip(list1, list2):
result.append(len(set(test_list1) & set(test_list2)) / float(len(set(test_list1) | set(test_list2))) * 100)
print(sum(result) / len(result))
You can try你可以试试
print(sum([sum([1 if x == list2[inx1][inx2] else 0 for inx2, x in enumerate(i)]) for inx1, i in enumerate(list1)]) / sum([len(i) for i in list1]) * 100)
That output那output
43.75
This is counting the matches between the inner lists of list1 and list2 and for every match, it will count it and on miss-match it won't.这是计算 list1 和 list2 的内部列表之间的匹配,并且对于每个匹配,它都会计算它,而在未匹配时它不会。 Then it's dividing the results with the count of all the elements in the list1 and multiply it by 100.然后将结果除以 list1 中所有元素的计数,然后将其乘以 100。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.