Konstantin
Konstantin

Reputation: 3123

How can I calculate the mode of a floating point array in Ruby?

I have an array of floating point data, I would like to pick out the most probable value. It is called "mode" in descriptive statistics. How can I calculate it in Ruby, or with the help of a gem.

Upvotes: 0

Views: 851

Answers (3)

ThirtySixthSpan
ThirtySixthSpan

Reputation: 11

DescriptiveStatistics adds methods to the Enumerable module to allow easy calculation of basic descriptive statistics of Numeric sample data in collections that have included Enumerable such as Array, Hash, Set, and Range.

> require 'descriptive_statistics'
> [0.0, 0.1, 0.2, 0.1, 0.3, 0.3, 0.1].mode
=> 0.1

Upvotes: 1

sawa
sawa

Reputation: 168229

[0.0, 0.1, 0.2, 0.1, 0.3, 0.3, 0.1]
.group_by{|e| e}.max_by{|k, v| v.length}.first
# => 0.1

Upvotes: 1

infused
infused

Reputation: 24337

The following will work for bimodal and multimodal datasets, but only returns a single value. For bimodal/multimodal datasets it always returns the value that occurs first in the array.

# returns 1.0
a = [1.0, 1.0, 2.0, 2.0, 3.0]
a.max_by { |x| a.count(x) }

You can also try the easystats gem. It adds a .mode method to Arrays (among other methods), but it returns nil for bimodal or multimodal datasets.

require 'easystats'

# returns 1.0
a = [1.0, 1.0, 2.0, 3.0]
a.mode 

# returns nil
a = [1.0, 1.0, 2.0, 2.0, 3.0]
a.mode

Upvotes: 0

Related Questions