简体   繁体   中英

Python , how do I get the percentage accuracy between 2 list of lists

Supoose I have 2 matrices (list of lists)

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%

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

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. Then it's dividing the results with the count of all the elements in the list1 and multiply it by 100.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM