473,698 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6925
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...@lexic on.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
2143
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 code or other people's code you have come across would be appreciated. I am also trying to identify the likelihood nad frequency of scenarios where smart pointer solutions would not be appropriate, i.e. for some reason such as performance or...
21
2508
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 contains M by 3 matrix of alpha-numeric keys. Where M is number of rows and 3 is number of columns.
1
3675
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 0 1 1 0 0
19
3138
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 not sure where to start. The only example I have to work with is if you ran the program say: File list contains: This is a test.
2
2837
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 number of occurrences and its value. eg Input: 0, 2, 5, 2, 23, 11, 11, 23, 15, 17, 25, 11, 23, 18, 23 (Ctrl+z) Output: Value is 23 Occurences is 4 For the input I have written this code:
2
2444
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 of the values in class intervals of 0 - 1.99, 2 - 3.99, 4 - 5.99 etc. My attempts do not give me the proper results. especially for the first class interval 0 - 1.99. HELP !!! My program #include<stdio.h> #define maxrl 10 #define bins 10
0
2864
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 invoice for october for a certain customer - i want to count how many other invoices appear are for this customer in the 12 months from that invoice date. Similarly, if there's an invoice in november, that should show the number of occurences 12 months...
1
2552
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 word in pages outputed, and so on. I dont know where to start, if anyone could post some sample code for me to see what i need to do, that would be greatly appreciated.
1
4938
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 not supposed to store the actual value in an array. Please help me with this problem. Thank you Guinness #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std;
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.