473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

frequency count or number of occurences of a number in an array

Dear all,

I'm new to Python. I have a file (an image file actually) that I need
to read pixel by pixel. It's an 8-bit integer type. I need to get the
statistics like mean, standard deviation, etc., which I know a little
bit already from reading numpy module. What I want to know is how to
get the number of occurences of numeric element in an array. Say,

b = array(([2, 2, 3, 4, 5, 5])

b.count(2) certainly does not work. Is there any more efficient way
other than converting this as string characters? My data will produce
a fairly large array like 400x400 = 160000 values. Hoping you guys can
help me.

Larry
Mar 12 '08 #1
5 6914
On Mar 12, 10:26 am, Larry <larry.cebu...@gmail.comwrote:
I'm new to Python. I have a file (an image file actually) that I need
to read pixel by pixel. It's an 8-bit integer type. I need to get the
statistics like mean, standard deviation, etc., which I know a little
bit already from reading numpy module. What I want to know is how to
get the number of occurences of numeric element in an array. Say,
Something like this should do the job:

histogram = [0] * 256
for x in my_array:
histogram[x] += 1

--
Paul Hankin
Mar 12 '08 #2
Hey Larry,

that one is fairly easy:
>>from array import array
array('i', [1, 2, 3, 4, 5, 1, 2])
def count(x, arr):
cpt = 0 # declare a counter variable
for el in arr: # for each element in the array
if el == x: # when it is equal to the 'x' value
cpt+=1 # increment the counter variable by one
return cpt # return the counter after the loop
>>count(1,a)
2

I'm pretty sure there must be an easier way though :)
On 12 mar, 06:26, Larry <larry.cebu...@gmail.comwrote:
Dear all,

I'm new to Python. I have a file (an image file actually) that I need
to read pixel by pixel. It's an 8-bit integer type. I need to get the
statistics like mean, standard deviation, etc., which I know a little
bit already from reading numpy module. What I want to know is how to
get the number of occurences of numeric element in an array. Say,

b = array(([2, 2, 3, 4, 5, 5])

b.count(2) certainly does not work. Is there any more efficient way
other than converting this as string characters? My data will produce
a fairly large array like 400x400 = 160000 values. Hoping you guys can
help me.

Larry
Mar 12 '08 #3
On Mar 12, 10:29 pm, Bernard <bernard.ch...@gmail.comwrote:
Hey Larry,

that one is fairly easy:
>from array import array
array('i', [1, 2, 3, 4, 5, 1, 2])
def count(x, arr):

cpt = 0 # declare a counter variable
for el in arr: # for each element in the array
if el == x: # when it is equal to the 'x' value
cpt+=1 # increment the counter variable by one
return cpt # return the counter after the loop>>count(1,a)

2
Hey Bernard, you have just laboriously reinvented the count method:
>>from array import array
a = array('i', [1, 2, 3, 4, 5, 1, 2])
a.count(1)
2
>>>
which Larry has already said doesn't do the job -- the job is to
create a histogram!!
Mar 12 '08 #4
Thanks to all those who replied to this post. I'm gonna try your
suggestions. They are a great help.
Mar 13 '08 #5
d'oh!

On 12 mar, 07:58, John Machin <sjmac...@lexicon.netwrote:
On Mar 12, 10:29 pm, Bernard <bernard.ch...@gmail.comwrote:
Hey Larry,
that one is fairly easy:
>>from array import array
>>array('i', [1, 2, 3, 4, 5, 1, 2])
>>def count(x, arr):
* * * * cpt = 0 # declare a counter variable
* * * * for el in arr: # for each element in the array
* * * * * * * * if el == x: # when it is equal to the 'x' value
* * * * * * * * * * * * cpt+=1 # increment thecounter variable by one
* * * * return cpt # return the counter after the loop>>count(1,a)
2

Hey Bernard, you have just laboriously reinvented the count method:
>from array import array
a = array('i', [1, 2, 3, 4, 5, 1, 2])
a.count(1)
2

which Larry has already said doesn't do the job -- the job is to
create a histogram!!
Mar 13 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
21
by: rccf6178 | last post by:
Hi all, Do anyone know how to write a C program to count the frequency of a certain key pattern in a given datafile? Input: • Three alpha-numeric keys separated by space • A data file...
1
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 ...
19
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm...
2
by: nick048 | last post by:
Hi, I am a new user of C++ and I am learning this language. I want to resolve this example: 1) I have an indefinite sequence of integer (from 0 to 36) as Input; 2) I want to know the maximum...
2
by: oormilarev | last post by:
dear all i want and as fast as possible a working C program for the following problem. i have an array values consisting of 'n' float values say ranging from 0 - 18. i want to get the frequencies...
0
by: uninvitedm | last post by:
Heya I've got a table of invoices, which have dates and customer_id's. What I need to get is the number of occurances for this customer for a 12-month range window. For example, if there's an...
1
by: ADHaughton | last post by:
i have an array pages = ; the user is asked to enter a word 'w', which is then searched for in the above array. I want the frequency of that word in pages outputed, then the frequency of the same...
1
by: shgu0501 | last post by:
hi I'm supposed to do a frequency array and find mode,median and count, but without storing the actual data in an array. I am able to build a frequency array but I can't return the mode since I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.