473,732 Members | 2,227 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 6927
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
2152
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
2510
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
3676
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
3139
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
2838
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
2447
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
2866
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
2556
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
4939
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
8946
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...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.