Last active
June 5, 2019 13:17
-
-
Save edvardm/0eedf9e95f9cc21bbad6d8ab52ac29a1 to your computer and use it in GitHub Desktop.
Simple statistics module
This file contains hidden or 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
# Simple stats module. For large sets of data, check Numpy instead | |
from statistics import mean, median_low, stdev, mode | |
def stat(lst): | |
if not lst: | |
return None | |
_min = min(lst) | |
_max = max(lst) | |
return { | |
'mean': mean(lst), | |
'median': median_low(lst), | |
'mode': mode(lst), | |
'min': _min, | |
'max': _max, | |
'stdev': stdev(lst), | |
'n': len(lst), | |
'range': _max - _min | |
} | |
def fmt_stat(lst): | |
values = stat(lst) | |
if not values: | |
return 'N/A, empty data set' | |
return 'n: {n}, mean/stdev: {mean:.3f}/{stdev:.2f}, median/mode: {median}/{mode}, min/max: {min}/{max}, range: {range}'.format(**values) | |
# Example usage: | |
# lst = [1, 2, 6, 2, 5, 2, 6, 7, 23, 5, 31, 11, 3] | |
# print(fmt_stat(lst)) # => n: 13, mean/stdev: 8.000/9.00, median/mode: 5/2, min/max: 1/31, range: 30 | |
# print(fmt_stat([])) # => N/A, empty data set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment