-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickstats
executable file
·67 lines (56 loc) · 1.65 KB
/
quickstats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# Given a list of numbers, provides some summary statistics and prints
# a histogram.
# Dependencies:
# needs my waterworks library:
# https://github.com/dmcc/waterworks
# Author: David McClosky
# Homepage: http://zorglish.org
# Code: http://github.com/dmcc
import fileinput
import AIMA
from waterworks.Strings import try_parse_float
import Histogram
nums = []
for line in fileinput.input():
nums.extend(try_parse_float(piece, None) for piece in line.split())
nums = [num for num in nums if num is not None]
def pretty_percent(count):
percent = 100 * count / len(nums)
return '%.1f%%' % percent
def summary_func_describer(func):
def new_func(nums):
val = func(nums)
count = nums.count(val)
return '%s [%d, %s]' % (val, count, pretty_percent(count))
return new_func
def uniq(nums):
num_uniq = len(set(nums))
return '{} [{}]'.format(num_uniq, pretty_percent(num_uniq))
def count_zeros(nums):
num_zeros = nums.count(0)
return '{} [{}]'.format(num_zeros, pretty_percent(num_zeros))
funcs = (
('min', summary_func_describer(min)),
('max', summary_func_describer(max)),
('mean', summary_func_describer(AIMA.mean)),
('mode', summary_func_describer(AIMA.mode)),
('median', summary_func_describer(AIMA.median)),
('stddev', AIMA.stddev),
('sum', sum),
('num uniq', uniq),
('num 0s', count_zeros),
('len', len),
)
for name, func in funcs:
try:
result = func(nums)
except Exception as e:
result = e
print(f"{name:>10}: {result}")
print(' histogram:')
try:
print(Histogram.histogramify(nums))
except Exception as e:
print(e)
raise