Sha256: 4f1d886d6db111de4f3850e6928bad6c6265b203ecd905059efd17b37436dac6
Contents?: true
Size: 995 Bytes
Versions: 1
Compression:
Stored size: 995 Bytes
Contents
# Convert quantitative data to categorical data. # http://saedsayad.com/binning.htm # E.g., ages in years to 5 groups: # * < 10 # * 11 - 20 # * 21 - 30 # * 31 - 40 # * > 40 class RailsDataExplorer module Utils class DataBinner def initialize(*args) @max = -Float::INFINITY @bin_specs = [*args].compact.uniq.sort.map { |e| case e when Numeric @max = [@max, e].max { :label => "#{ e.to_s } or less", :lte => e } else raise "Handle this bin_spec: #{ e.inspect }" end } @bin_specs << { :label => "> #{ @max }", :gt => @max } end def bin(value) unless value.is_a?(Numeric) raise(ArgumentError.new("Wrong type of value, numeric expected, got: #{ value.inspect }")) end bin = @bin_specs.detect { |bs| (bs[:lte] && value <= bs[:lte]) || (bs[:gt] && value > bs[:gt]) } bin[:label] end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rails-data-explorer-0.1.0 | lib/rails-data-explorer/utils/data_binner.rb |