Created
July 26, 2014 00:51
-
-
Save thalesmello/d71f03553c90e83051e8 to your computer and use it in GitHub Desktop.
histogram puts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def histogram(data, bins = 10, length = 100) | |
min, max = data.minmax | |
mod = max - min | |
histog = data.group_by { |v| ((v - min).to_f / mod * bins).to_i }.map { |bin, values| [bin, values.count] }.to_h | |
max_value = histog.map { |_, value| value }.max | |
histog_array = [0] * bins | |
histog = histog.each { |bin, value| histog_array[bin] = (value.to_f / max_value * length).to_i } | |
puts histog_array.to_s | |
puts min.to_s + " " * length + max_value.to_s | |
histog_array.each { |value| puts "*" * value.to_i } | |
puts max | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment