How many times a number appears in a numpy array

2024/10/4 15:33:20

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.

Answer

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.

https://en.xdnf.cn/q/70589.html

Related Q&A

python: How to remove values from 2 lists based on whats in 1 list

I have 2 lists of numbers, one called xVar and the other called yVar. I will use these 2 elements to plot X and Y values on a graph. They both have the same number of elements. Normally, I would jus…

merge two dataframe columns into 1 in pandas

I have 2 columns in my data frame and I need to merge it into 1 single columnIndex A Index B 0 A 0 NAN 1 NAN 1 D 2 B 2 …

Upsample and Interpolate a NumPy Array

I have an array, something like:array = np.arange(0,4,1).reshape(2,2)> [[0 12 3]]I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an…

How to extract text from table in image?

I have data which in a structured table image. The data is like below:I tried to extract the text from this image using this code:import pytesseract from PIL import Imagevalue=Image.open("data/pic…

numpy.savetxt tuple index out of range?

Im trying to write a few lines into a text file, and heres the code I used:import numpy as np# Generate some test data data = np.arange(0.0,1000.0,50.0)with file(test.txt, w) as outfile: outfile.w…

Retrieve list of USB items using Python

How can I retrieve the items plugged into the computer through USB using python? Ive searched around on here and found some old examples which dont appear to work anymore as they are over 5 years old.…

History across ipdb sessions

This question has been asked before, but I couldnt find a good answer. So, I am trying to ask again.I would like my ipdb to remember commands across sessions. Right now, it can pull up commands execute…

Python Distributed Computing (works)

Im using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this?sock.pyfrom socket import socket from socket import AF_INET from socket import…

Django - Stream request from external site as received

How can Django be used to fetch data from an external API, triggered by a user request, and stream it directly back in the request cycle without (or with progressive/minimal) memory usage?BackgroundAs…

django rest framework - always INSERTs, never UPDATES

I want to be able to UPDATE a user record by POST. However, the id is always NULL. Even if I pass the id it seems to be ignoredView Code:JSON POSTED:{"id": 1, "name": "Craig Ch…