-1

I have three lists:

list1 = [1, 5, 5, 4, 2]
list2 = [1, 4, 5, 3, 4]
list3 = [2, 5, 4, 3, 4]

I want to return the most occurring number in each column so for example:

  • Column 1 will return 1 since there are {1, 1, 2} and 1 is most occurring
  • Column 2 will return 5 since there are {5, 4, 5} and 5 is most occurring
  • Column 3 will return 5.

For these lists, I want to get [1, 5, 5, 3, 4].

The list will always have equal length.

pault
  • 41,343
  • 15
  • 107
  • 149
Fran
  • 98
  • 1
  • 12
  • 1
    What if there are no 'most occurring' numbers? Are the lists always of equal length? – zwer Apr 12 '18 at 20:27
  • 1
    Can you show what you've tried? Right now you are basically asking us to do your "assignment" – MooingRawr Apr 12 '18 at 20:27
  • @zwer yes the list will always have equal length. If there are no recurring number, I need to compare the accuracy of each list (accuracy is produced based on three types of machine learning algorithm from sci-kit library) and choose the number in the column with the highest accuracy. – Fran Apr 12 '18 at 20:29

1 Answers1

2

Here's a solution using scipy.stats.mode:

from scipy.stats import mode
m = mode([list1, list2, list3])
print(m)
#ModeResult(mode=array([[1, 5, 5, 3, 4]]), count=array([[2, 2, 2, 2, 2]]))

And if you want just the most frequent values as a list:

print(list(m.mode[0]))
#[1, 5, 5, 3, 4]
pault
  • 41,343
  • 15
  • 107
  • 149