Find Mode Value From A List Of Float Data

I have a list of data that are float value.Example {0.0, 0.87, 0.58, 0.87, 0.59, 0.87, 1.0}The mode is 0.87 (appear 3 times)What is the algorithm to get the mode value?
[196 byte] By [anna_18a] at [2007-9-29 5:47:41]
# 1

Find the frequency of each item!!!then find the maximum of frequencies(u can use any good sort algorithm), the number corresponding to this frequncies will be mode.

all u need to be carefull is that u can keep frequency and its candidate bounded so that when u find frequency u know what the number is and when u know number u also know what its frequency is in the list;)

RanjanNa at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 2

Ya. I had an idea how to find mode value with integer value.

As i done previously, i count the frequency and get the mode value.

The algorithm i used last time is:

1) find maximum value, max

2) create an array to keep frequency. freq=new int(max+1)

3) count the list of data and increment 1 at the index of array.

Example: A list {1,2,3,2,5,2,2},

Create an array with index 0-5.

Value 1 is read fom the list, go to the INDEX 1 at the array, add frequency 1 at the array.Then read 2.....

..... Finally, we can find out the frequency from the array and get the mode. Right?

But in this case, the data is in float value. So i dont't know how to keep the frequency in the array. Seems the index of array cann't declare as (0, 0.25, 0.87, 1).

anna_18a at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 3
have a look at a java.util.Mapand java.util.Collectionnote also that you need to wrap a float (simple type) in a Float (Class)in order to store it in a CollectionregardsSpieler
spielera at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 4

u can have one dedicated class like this

class freqkeeper

{

float value;

int frequncy;

};

class Mode

{

freqkeeper[0];

};

res u'r methods can increment frequncey whenever they meet the same number.freq[0] will give u value and its frequency!!!did u get it;)

RanjanNa at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 5

I'd make a binary search tree with nodes that contain the float and frequency.. Go down the tree with each float. If you find it, increment the frequency. If you dont, create a new node with that float and frequency=1. Insert time=log(n) for any one float, and you can get your frequencies sorted by float in linear time.

dforbua at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 6

Personally I would just sort the array, then write some code that went through the array, observing groups of elements with the same value and keeping track of which group was largest and which value was associated with that group. No intermediate storage required. At least not unless you need to consider the possibility of multiple modes -- worst case, all the entries are different and everything is a mode.

DrClapa at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 7

This code should be tested! The suggestion of using a BST (which by the way could have a very bad runtime, unless it was balanced) or other object data structures is inefficient.This problem does not require anything as complex. This code runs in O(nlogn) time.

float[] data = ...;

java.util.Arrays.sort(data);

int longest = 0;

float mode = 0;

int i = 1;

while (i < data.length) {

int temp = 1;

while (i < data.length) {

if (data[ i ] == data[i-1]) {

temp++;

i++;

}

else break;

}

if (temp > longest) {

longest = temp;

mode=data[i-1];

}

i++;

}

rkippena at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 8
The code had been tested successfully! Thanks!
anna_18a at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 9

> The suggestion of using a

> BST (which by the way could have a very bad runtime,

> unless it was balanced) or other object data

> structures is inefficient.This problem does not

> require anything as complex. This code runs in

> O(nlogn) time.

Well, I was gonna come in and blast you for doggin my idea, but the more I think about it, you really do have nice simple efficient code there, I like it...

But, to defend my original reply, the only inefficiency I can see with the tree solution is all the node objects that need to be generated, they will take the extra cpu to instantiate, and a little extra memory since I'm holding frequencies as well as the float.. If you have BST code already, then it really isn't any harder to code than your array implementation.

There's a lot of benefits from having your data in tree form that you don't have in arrays. A tree could still be a better idea if you will need to add/remove/search nodes later. A tree is actually very easy to keep balanced, even if your data starts off sorted. I suppose whether to use arrays or trees would depend on how you need to operate on this data later. If it's a one time calculation, and you're just gonna dump the data when you're done, then your simple code will be a better answer.

dforbua at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 10

> I have a list of data that are float value.

> Example {0.0, 0.87, 0.58, 0.87, 0.59, 0.87, 1.0}

> The mode is 0.87 (appear 3 times)

> What is the algorithm to get the mode value?

There is a slight problem with the question.

If there is a large cluster of values close to 0.87

and any clusters close to other values are small, we

might prefer to regard the mode to be 0.87, but if

each value is unique, or if some other value happens

to be duplicated by chance, the mode calculated with

floating point arithmetic might not be 0.87.

The answer is to divide the range of values into

groups, using some grouping interval, and work with

these. Choice of grouping interval is an art rather

than a science. In your case you could round to two

decimal places, but the best interval depends on the

data.

TerryMoorea at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...
# 11
> Well, I was gonna come in and blast you for doggin my idea, but the> more I think about it, you really do have nice simple efficient code > there, I like it...Flattery will get you everywhere.
rkippena at 2007-7-14 18:48:55 > top of Java-index,Other Topics,Algorithms...