Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News Sign UpSign In
| Download

Think Stats by Allen B. Downey Think Stats is an introduction to Probability and Statistics for Python programmers.

This is the accompanying code for this book.

Website: http://greenteapress.com/wp/think-stats-2e/

Views: 7088
License: GPL3
1
"""This file contains code for use with "Think Stats",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2014 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
from __future__ import print_function
9
10
import sys
11
from operator import itemgetter
12
13
import first
14
import thinkstats2
15
16
17
def Mode(hist):
18
"""Returns the value with the highest frequency.
19
20
hist: Hist object
21
22
returns: value from Hist
23
"""
24
return 0
25
26
27
def AllModes(hist):
28
"""Returns value-freq pairs in decreasing order of frequency.
29
30
hist: Hist object
31
32
returns: iterator of value-freq pairs
33
"""
34
return []
35
36
37
def main(script):
38
"""Tests the functions in this module.
39
40
script: string script name
41
"""
42
live, firsts, others = first.MakeFrames()
43
hist = thinkstats2.Hist(live.prglngth)
44
45
# test Mode
46
mode = Mode(hist)
47
print('Mode of preg length', mode)
48
assert mode == 39, mode
49
50
# test AllModes
51
modes = AllModes(hist)
52
assert modes[0][1] == 4693, modes[0][1]
53
54
for value, freq in modes[:5]:
55
print(value, freq)
56
57
print('%s: All tests passed.' % script)
58
59
60
if __name__ == '__main__':
61
main(*sys.argv)
62
63