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)

4
  • 1
    Have you tried something? Commented Dec 27, 2018 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 Commented Dec 27, 2018 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. Commented Dec 27, 2018 at 18:16
  • 1
    Take a look at numpy.unique. You can return counts. Commented Dec 27, 2018 at 18:17

2 Answers 2

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])
Sign up to request clarification or add additional context in comments.

2 Comments

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)
np.matrix is being deprecated and, in this case, np.array produces the matrix you are looking for.
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

7 Comments

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: 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?
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: 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
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.