Sha256: a42cc382ab74bb2e9087f6932be47f109a6d63148364d94f3d5308c08b1adad1
Contents?: true
Size: 787 Bytes
Versions: 2
Compression:
Stored size: 787 Bytes
Contents
class Array # Generates a hash mapping each unique element in the array to the # relative frequency, i.e. the probability, of it appearance. # # [:a, :b, :c, :c].probability #=> {:a=> 0.25, :b=>0.25, :c=>0.5} # # CREDIT: Brian Schröder def probability probs = Hash.new(0.0) size = 0.0 each do |e| probs[e] += 1.0 size += 1.0 end probs.keys.each{ |e| probs[e] /= size } probs end #-- # Old Definition ... # # def probability # arr = self.to_a # probHash = Hash.new # size = arr.size.to_f # arr.uniq.each do |i| # ct = arr.inject(0) do |mem,obj| # obj.eql?(i) ? (mem+1) : mem # end # probHash[i] = ct.to_f/size # end # probHash # end #++ end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
facets-glimmer-3.2.0 | lib/core/facets/array/probability.rb |
facets-3.1.0 | lib/core/facets/array/probability.rb |