7

I need to find a way to count how many times each number from 0 to 9 appears in a random matrix created using np.random.randint()

import numpy as np
p = int(input("Length of matrix: "))
m = np.random.randint(0,9,(p,p))
print(m)

For example if length of matrix = 4

  • [[3 4 6 5] [3 4 4 3] [4 2 4 8] [6 8 2 7]]

How many times does the number 4 appear? It should return 5.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Teddy
  • 75
  • 1
  • 1
  • 7

5 Answers5

10

You should be able to get this pretty simply:

list(m.flatten()).count(x)

Another option which is probably faster, is to use the numpy builtin count_nonzero():

np.count_nonzero(m == x)

Hooray builtin functions.

TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
3

You can use sum function:

In [52]: m = np.random.randint(0,9,(4,4))
In [53]: m
Out[53]: 
array([[8, 8, 2, 1],
       [2, 7, 1, 2],
       [8, 6, 8, 7],
       [5, 2, 5, 2]])

In [56]: np.sum(m == 8)
Out[56]: 4

m == 8 will return a boolean array contains True for each 8 then since python evaluates the True as 1 you can sum up the array items in order to get the number of intended items.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
2

If you want to get the frequency from all matrix elements, here's a simple solution using numpy.ndarray.flatten and collections.Counter:

import numpy as np
import collections

p = int(input("Length of matrix: "))
m = np.random.randint(0, 9, (p, p))
print(m)
print(collections.Counter(m.flatten()))

For example, when p=3 you'd get something like this:

[[8 4 8]
 [5 1 1]
 [1 1 1]]
Counter({1: 5, 8: 2, 4: 1, 5: 1})
BPL
  • 9,632
  • 9
  • 59
  • 117
1

You can flatten the matrix and then use the list count() method:

from collections import Counter
import numpy as np
p = int(input("Length of matrix: "))
m = np.random.randint(0,9,(p,p))
print(m)
flat = [item for sublist in m for item in sublist]
flat.count(4)
rofls
  • 4,993
  • 3
  • 27
  • 37
  • 2
    [Counting a list](http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python) is even easier than that: `flat.count(x)` is sufficient. – TemporalWolf Dec 09 '16 at 19:27
  • 2
    Also, numpy has a [flatten](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html): `list(m.flatten()).count(x)` – TemporalWolf Dec 09 '16 at 19:31
  • I'm curious which method is faster, since you still have to convert this to a list @TemporalWolf – rofls Dec 09 '16 at 21:22
  • Usually built-in functions are faster than custom ones. You're welcome to `timeit` – TemporalWolf Dec 09 '16 at 21:29
  • @TemporalWolf This is incorrect, NumPy functions will be much faster than built in Python functions if the array is of a relevant size. – Daniel Dec 09 '16 at 21:40
  • @Daniel I was including the numpy built-ins (like `flatten`), comparing them to rofls' custom list comprehension solution. – TemporalWolf Dec 09 '16 at 21:44
0

I would try numpy unique function with argument return_counts=True (see: https://numpy.org/doc/stable/reference/generated/numpy.unique.html).

import numpy as np
p = int(input("Length of matrix: "))
m = np.random.randint(0,9,(p,p))
# print(m)
un, nm = np.unique(m, return_counts = True)
# if number that you are looking for is 1 then:
print(nm[un==1])