Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > frequency count or number of occurences of a number in an array

Reply
Thread Tools

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

 
 
Larry
Guest
Posts: n/a
 
      03-12-2008
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
 
Reply With Quote
 
 
 
 
Paul Hankin
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 10:26 am, Larry <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Bernard
Guest
Posts: n/a
 
      03-12-2008
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 <(E-Mail Removed)> wrote:
> 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


 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 10:29 pm, Bernard <(E-Mail Removed)> wrote:
> 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!!
 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      03-13-2008
Thanks to all those who replied to this post. I'm gonna try your
suggestions. They are a great help.
 
Reply With Quote
 
Bernard
Guest
Posts: n/a
 
      03-13-2008
d'oh!

On 12 mar, 07:58, John Machin <(E-Mail Removed)> wrote:
> On Mar 12, 10:29 pm, Bernard <(E-Mail Removed)> wrote:
>
>
>
> > 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!!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 6.15 How can I print out a word-frequency or line-frequency summary? PerlFAQ Server Perl Misc 0 02-01-2011 11:00 AM
Count occurences of vars in array Vitaliy Yanchuk Ruby 15 07-25-2010 09:05 AM
count the number of occurences of each different word of a text Mr_Noob Perl Misc 19 12-28-2007 08:18 PM
Counting Frequency of Values in an Array (And Sorting by Frequency?) x1 Ruby 9 10-12-2006 04:04 PM
Is there a VAR to count the number of occurences of matching shrek11001 Perl Misc 6 10-28-2003 02:46 PM



Advertisments