473,410 Members | 1,952 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,410 software developers and data experts.

Modal value of an array

Hi

How can I find out the modal value in an array. That is the value
which occurs maximum time in the sequence ..

e.g. if my array has values like [2,3,2,2,2,4,2,2] definitely the
maximum time 2 occurs in the array. so this function should be able to
return 2 as a result ..

So is there any function in built in python which can do that ?
Thanks

Abhirup

Mar 29 '07 #1
10 2225
"da***********@gmail.com" <da***********@gmail.comwrites:
Hi

How can I find out the modal value in an array. That is the value
which occurs maximum time in the sequence ..

e.g. if my array has values like [2,3,2,2,2,4,2,2] definitely the
maximum time 2 occurs in the array. so this function should be able to
return 2 as a result ..
That's not the only case though. What do you expect to be returned for
an input of ["eggs", "beans", "beans", "eggs", "spam"] ?

Assuming you want *a* mode value, and any one will do (e.g. any of
"spam", "eggs" or "beans" is okay), I'd write it this way as a first
guess:
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
counts = [(foo.count(val), val) for val in set(foo)]
counts
[(2, 'eggs'), (1, 'beans'), (4, 'spam')]
>>sorted(counts)[-1]
(4, 'spam')
>>sorted(counts)[-1][1]
'spam'
>>foo = ["eggs", "beans", "beans", "eggs", "spam"]
counts = [(foo.count(val), val) for val in set(foo)]
sorted(counts)[-1][1]
'eggs'

--
\ "Anger makes dull men witty, but it keeps them poor." -- |
`\ Elizabeth Tudor |
_o__) |
Ben Finney
Mar 29 '07 #2
On Wed, 28 Mar 2007 20:40:22 -0700, da***********@gmail.com wrote:
Hi

How can I find out the modal value in an array. That is the value
which occurs maximum time in the sequence ..

e.g. if my array has values like [2,3,2,2,2,4,2,2] definitely the
maximum time 2 occurs in the array. so this function should be able to
return 2 as a result ..

So is there any function in built in python which can do that ?
No. You need to create a frequency table, then do a reverse-lookup on the
frequency table. Assuming your data is small, this should be plenty fast
enough.

def mode(data):
# create a frequency table
freq = {}
for x in data:
freq[x] = freq.get(x, 0) + 1
# find the maximum frequency
F = max(freq.values())
# return the items (one or more) with that frequency
modes = []
for x, f in freq.items():
if f == F:
modes.append(x)
return modes
>>mode([2,3,2,2,2,4,2,2])
[2]
>>mode([2,3,2,3,2,3,4,1])
[2, 3]
--
Steven.

Mar 29 '07 #3
On Mar 29, 4:40 am, "datta.abhi...@gmail.com"
<datta.abhi...@gmail.comwrote:
Hi

How can I find out the modal value in an array. That is the value
which occurs maximum time in the sequence ..

e.g. if my array has values like [2,3,2,2,2,4,2,2] definitely the
maximum time 2 occurs in the array. so this function should be able to
return 2 as a result ..

So is there any function in built in python which can do that ?

Thanks

Abhirup
With the same assumptions as Ben Finney, I came up with this:
>>import operator
foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
count = {}
for item in foo: count[item] = count.get(item, 0) +1
....
>>maxitem = max(count.items(), key= operator.itemgetter(1))
maxitem
('spam', 4)
>>>
I was trying to minimise the iterations through the list.

- Paddy.

Mar 29 '07 #4
Ben Finney <bi****************@benfinney.id.auwrote:
...
That's not the only case though. What do you expect to be returned for
an input of ["eggs", "beans", "beans", "eggs", "spam"] ?

Assuming you want *a* mode value, and any one will do (e.g. any of
"spam", "eggs" or "beans" is okay), I'd write it this way as a first
guess:
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
>>counts = [(foo.count(val), val) for val in set(foo)]
>>counts
[(2, 'eggs'), (1, 'beans'), (4, 'spam')]
>>sorted(counts)[-1]
(4, 'spam')
>>sorted(counts)[-1][1]
'spam'
A bit more directly:
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
max(foo, key=foo.count)
'spam'
Alex
Mar 29 '07 #5
Alex Martelli:
>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
max(foo, key=foo.count)
It's a very nice solution, the shortest too. But I think it's better
to develop your own well tested and efficient stats module (and there
is one already done that can be found around) and import it when you
need functions, instead of using similar onliners or re-writing code.
As you know your solution becomes rather slow if the list is quite
long, and it works with lists only.
This uses more memory but it's probably much faster for longer
interables:

from collections import defaultdict

def mode(seq):
freqs = defaultdict(int)
for el in seq:
freqs[el] += 1
return max(freqs.itervalues())
Generally you may want to know what's the mode element(s) too:

def mode2(seq):
freqs = defaultdict(int)
for el in seq:
freqs[el] += 1
maxfreq = max(freqs.itervalues())
mode_els = [el for el,f in freqs.iteritems() if f == maxfreq]
return maxfreq, mode_els

foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
print mode(foo)
print mode2(foo)

Bye,
bearophile

Mar 29 '07 #6
On Mar 29, 8:49 am, a...@mac.com (Alex Martelli) wrote:
Ben Finney <bignose+hates-s...@benfinney.id.auwrote:

...
That's not the only case though. What do you expect to be returned for
an input of ["eggs", "beans", "beans", "eggs", "spam"] ?
Assuming you want *a* mode value, and any one will do (e.g. any of
"spam", "eggs" or "beans" is okay), I'd write it this way as a first
guess:
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
>>counts = [(foo.count(val), val) for val in set(foo)]
>>counts
[(2, 'eggs'), (1, 'beans'), (4, 'spam')]
>>sorted(counts)[-1]
(4, 'spam')
>>sorted(counts)[-1][1]
'spam'

A bit more directly:
>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
max(foo, key=foo.count)

'spam'

Alex
This doesn't call foo.count for duplicate entries by keeping a cache
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
def cachecount(x, cache={}):
.... return cache.setdefault(x, foo.count(x))
....
>>max(foo, key=cachecount)
'spam'
>>cachecount.func_defaults
({'eggs': 2, 'beans': 1, 'spam': 4},)
>>>
- Paddy.

Mar 29 '07 #7
Paddy <pa*******@googlemail.comwrote:
...
A bit more directly:
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
>>max(foo, key=foo.count)
'spam'

Alex

This doesn't call foo.count for duplicate entries by keeping a cache
>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
def cachecount(x, cache={}):
... return cache.setdefault(x, foo.count(x))
...
>max(foo, key=cachecount)
'spam'
>cachecount.func_defaults
({'eggs': 2, 'beans': 1, 'spam': 4},)
>>
If you're willing to do that much extra coding to save some work (while
still being O(N squared)), then the further small extra needed to be
O(N) starts looking good:

counts = collections.defaultdict(int)
for item in foo: counts[item] += 1
max(foo, key=counts.get)
Alex
Mar 30 '07 #8
On Mar 30, 2:58 am, a...@mac.com (Alex Martelli) wrote:
Paddy <paddy3...@googlemail.comwrote:

...
A bit more directly:
>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
>max(foo, key=foo.count)
'spam'
Alex
This doesn't call foo.count for duplicate entries by keeping a cache
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
>>def cachecount(x, cache={}):
... return cache.setdefault(x, foo.count(x))
...
>>max(foo, key=cachecount)
'spam'
>>cachecount.func_defaults
({'eggs': 2, 'beans': 1, 'spam': 4},)

If you're willing to do that much extra coding to save some work (while
still being O(N squared)), then the further small extra needed to be
O(N) starts looking good:

counts = collections.defaultdict(int)
for item in foo: counts[item] += 1
max(foo, key=counts.get)

Alex
Yeh, My first answer is like that but I had to play around with your
original to try and 'fix' the idea in my head - it might be useful
someday.
:-)

- Paddy.

Mar 30 '07 #9
En Thu, 29 Mar 2007 19:44:56 -0300, Paddy <pa*******@googlemail.com>
escribió:
On Mar 29, 8:49 am, a...@mac.com (Alex Martelli) wrote:
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
max(foo, key=foo.count)

'spam'

This doesn't call foo.count for duplicate entries by keeping a cache
>>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
def cachecount(x, cache={}):
... return cache.setdefault(x, foo.count(x))
Unfortunately it does, because all arguments are evaluated *before* a
function call, so you gain nothing.

--
Gabriel Genellina

Mar 30 '07 #10
On Mar 30, 10:17 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 29 Mar 2007 19:44:56 -0300, Paddy <paddy3...@googlemail.com>
escribió:
On Mar 29, 8:49 am, a...@mac.com (Alex Martelli) wrote:
>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
max(foo, key=foo.count)
'spam'
This doesn't call foo.count for duplicate entries by keeping a cache
>>foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
def cachecount(x, cache={}):
... return cache.setdefault(x, foo.count(x))

Unfortunately it does, because all arguments are evaluated *before* a
function call, so you gain nothing.

--
Gabriel Genellina
I had to experiment to find out what you meant but I finally got it.
that call to foo.count in the setdefault is *always* called. Forgive
my senility.

- Paddy.

Mar 31 '07 #11

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

Similar topics

3
by: Sharon | last post by:
Hi y'all, I'm trying to figure out how to tackle this problem: I have an XML table with a cool grid in which users can select a table row. When they right-click on a cell, they get a modal dialog...
2
by: Matt | last post by:
I want to know how to submit the form data to a modal dialog window? The following is page1.asp, and when the user clicks submit button, it will post the form data to page2.asp by opening a new...
1
by: stellabein | last post by:
Hi friends, I am very very new to programing. i have a one main window from that window i am opening one modal window using showmodaldialog(m.jp...). in that modal window i have a form. when i...
2
by: =?Utf-8?B?TmF0aGFuIFdpZWdtYW4=?= | last post by:
Hi, I am wondering why the .NET Framework is quite different from Win32 API when it comes to displaying system modal message boxes. Consider the four following types of system modal message...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.