1

I'm trying to find and print the most common number in a matrix, using numpy if possible.

This list was given (made it a matrix using numpy.matrix(list)):

import numpy as np
list = [[2,4,1,6,3], [2,4,1,8,4], [6,5,4,3,2], [6,5,4,3,4], [1,2,3,4,5]]
matrix=np.matrix(list)

for this example i should be getting: 4 (as it is the most common number)

cs95
  • 379,657
  • 97
  • 704
  • 746
Omer
  • 41
  • 1
  • 6
  • 1
    Have you tried something? – cs95 Dec 27 '18 at 18:02
  • thought about flattening the matrix and running a loop counting each number between 1-9, though it's too complex and i'm sure there's a simpler solution i'm missing – Omer Dec 27 '18 at 18:16
  • 1
    Don't use `np.matrix`. It's got all sorts of weird compatibility issues, it's deprecated, and people often find that they want the exact opposite of the behavior they asked for by using it. – user2357112 Dec 27 '18 at 18:16
  • 1
    Take a look at `numpy.unique`. You can return counts. – busybear Dec 27 '18 at 18:17

2 Answers2

3

Given:

>>> import numpy as np
>>> LoL = [[2,4,1,6,3], [2,4,1,8,4], [6,5,4,3,2], [6,5,4,3,4], [1,2,3,4,5]]
>>> matrix=np.array(LoL)
>>> matrix
[[2 4 1 6 3]
 [2 4 1 8 4]
 [6 5 4 3 2]
 [6 5 4 3 4]
 [1 2 3 4 5]]

You can do:

>>> np.argmax(np.bincount(matrix.flat))
4

Or,

u, c = np.unique(your_lst, return_counts=True)
u[c.argmax()]
# 4

If you wanted to do this without numpy or any import to count the most frequent entry in a list of lists, you can use a dictionary to count each element from a generator that is flattening your list of lists:

cnts={}
for e in (x for sl in LoL for x in sl):
    cnts[e]=cnts.get(e, 0)+1

Then sort by most frequent:

>>> sorted(cnts.items(), key=lambda t: t[1], reverse=True)
[(4, 7), (2, 4), (3, 4), (1, 3), (5, 3), (6, 3), (8, 1)]

Or, just use max if you only want the largest:

>>> max(cnts.items(), key=lambda t: t[1])
dawg
  • 98,345
  • 23
  • 131
  • 206
  • wow! worked great, though instead of np.array I used np.matrix and it still worked. so i will ask you as i asked the others... why shouldnt i use it? (tried to upvote though i'm still new) – Omer Dec 27 '18 at 18:46
  • `np.matrix` is being deprecated and, in this case, `np.array` produces the matrix you are looking for. – dawg Dec 27 '18 at 18:48
1

You don't need an intermediate matrix. You can directly flatten your list to get a single list and use bincount. It returns a list where the frequency of each number is given at the index position which corresponds to the number. That's why you use argmax to get the corresponding index

import numpy as np
listt = [[2,4,1,6,3], [2,4,1,8,4], [6,5,4,3,2], [6,5,4,3,4], [1,2,3,4,5]]
flat = [i for j in listt for i in j]
counts = np.bincount(flat)
print (np.argmax(counts))
# 4
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • 1
    `sum(listt, [])` is one of the worst possible ways to flatten a list, and it doesn't work at all with a NumPy matrix. Also, `bincount` does not return a list. – user2357112 Dec 27 '18 at 18:15
  • @user2357112: The user has started by initialising a list and then later converting to numpy matrix. The conversion to NumPy matrix is not needed here. I replaced `sum` part with a list comprehension. And what is this argument about bincount not returning alist? What is its relevance to `sum`? – Sheldore Dec 27 '18 at 18:22
  • worked great! i'd like to understand fully: how did the sum(list,[]) flattend the matrix? is there any way to do it without flattening? – Omer Dec 27 '18 at 18:23
  • @Omer: I removed the sum part for flattening a list and replaced it by list comprehension as some user didn't like it for being inefficient. It basically takes all the `[]` instances and sums them. If you have two lists a = [1,2,3] and b = [4,5,6] then a+b merges the two lists to give [1,2,3,4,5,6]. This is what sum(listt, []) was doing – Sheldore Dec 27 '18 at 18:25
  • @user2357112: I am not using it on Numpy matrix. I really don't understand your downvote apart from the `sum` argument being inefficient which I took care of in my edit. As coldspeed also said, numpy matrix was not needed here. – Sheldore Dec 27 '18 at 18:26
  • thank you very much!! tried to upvote but still new so it doesnt count yet. i agree using a matrix is pretty useless here, though it is what I was requested so... – Omer Dec 27 '18 at 18:42
  • @Omer: You are welcome. Glad you find the answer helpful – Sheldore Dec 27 '18 at 18:56