epitools 0.5.17 → 0.5.18
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/epitools/core_ext.rb +1 -0
- data/lib/epitools/core_ext/array.rb +44 -0
- metadata +2 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.18
|
data/lib/epitools/core_ext.rb
CHANGED
@@ -5,6 +5,7 @@ require 'epitools'
|
|
5
5
|
require 'epitools/core_ext/object'
|
6
6
|
require 'epitools/core_ext/string'
|
7
7
|
require 'epitools/core_ext/array'
|
8
|
+
require 'epitools/core_ext/histogram'
|
8
9
|
require 'epitools/core_ext/enumerable'
|
9
10
|
require 'epitools/core_ext/hash'
|
10
11
|
require 'epitools/core_ext/numbers'
|
@@ -107,6 +107,50 @@ class Array
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
+
#
|
111
|
+
# Takes an array of numbers, puts them into equal-sized
|
112
|
+
# buckets, and counts the buckets (aka. A Histogram!)
|
113
|
+
#
|
114
|
+
# Examples:
|
115
|
+
# [1,2,3,4,5,6,7,8,9].histogram(3) #=> [3,3,3]
|
116
|
+
# [1,2,3,4,5,6,7,8,9].histogram(2) #=> [4,5]
|
117
|
+
# [1,2,3,4,5,6,7,8,9].histogram(2, ranges: true)
|
118
|
+
# #=> {
|
119
|
+
# 1.0...5.0 => 4,
|
120
|
+
# 5.0...9.0 => 5
|
121
|
+
# }
|
122
|
+
#
|
123
|
+
def histogram(n_buckets=10, options={})
|
124
|
+
|
125
|
+
use_ranges = options[:ranges] || options[:hash]
|
126
|
+
|
127
|
+
min_val = min
|
128
|
+
max_val = max
|
129
|
+
range = (max_val - min_val)
|
130
|
+
bucket_size = range.to_f / n_buckets
|
131
|
+
buckets = [0]*n_buckets
|
132
|
+
|
133
|
+
# p [range, bucket_size, buckets, min_val, max_val]
|
134
|
+
|
135
|
+
each do |e|
|
136
|
+
bucket = (e - min_val) / bucket_size
|
137
|
+
bucket = n_buckets - 1 if bucket >= n_buckets
|
138
|
+
# p [:e, e, :bucket, bucket]
|
139
|
+
buckets[bucket] += 1
|
140
|
+
end
|
141
|
+
|
142
|
+
if use_ranges
|
143
|
+
ranges = (0...n_buckets).map do |n|
|
144
|
+
offset = n*bucket_size
|
145
|
+
(min_val + offset) ... (min_val + offset + bucket_size)
|
146
|
+
end
|
147
|
+
Hash[ ranges.zip(buckets) ]
|
148
|
+
else
|
149
|
+
buckets
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
110
154
|
end
|
111
155
|
|
112
156
|
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: epitools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.5.
|
5
|
+
version: 0.5.18
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- epitron
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
type: :development
|
@@ -130,4 +130,3 @@ signing_key:
|
|
130
130
|
specification_version: 3
|
131
131
|
summary: Not utils... METILS!
|
132
132
|
test_files: []
|
133
|
-
has_rdoc:
|