Search code examples
rubyarraysfloating-pointstatisticsmode

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.


Solution

  • [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