python/nondist/sandbox/statistics statistics.py, 1.12, 1.13 test_statistics.py, 1.3, 1.4
Update of /cvsroot/python/python/nondist/sandbox/statistics In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2748 Modified Files: statistics.py test_statistics.py Log Message: * removed nlargest() and nsmallest() which are now in the heapq module. * removed product() which did not fit with the rest of the module. * merged mean() and variance() into one function returning (count, mean, variance) after a single pass. * verified that the functions work with Decimal objects as well as floats. Index: statistics.py =================================================================== RCS file: /cvsroot/python/python/nondist/sandbox/statistics/statistics.py,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- statistics.py 2 Apr 2004 06:09:45 -0000 1.12 +++ statistics.py 5 Sep 2004 11:42:35 -0000 1.13 @@ -1,52 +1,8 @@ -"""statistics.py A collection of functions for summarizing data - -The module focuses on everyday data reduction functions and omits more -sophisticated statistical tools. - -Unless otherwise noted, each function is designed to accept any iterable -and make only a single pass over the data. This makes the tools suitable -for use with generator expressions and keeps them as memory friendly as -possible. Except for median() and select(), the use of iterators means -that the dataset can be processed one element at a time without bringing -it all into memory. - -The functions, nlargest and nsmallest, are also designed to use the fewest -possible data comparisions and the data elements may be non-numerical (such -as strings) as long they support __cmp__ or __lt__. - -Written and maintained by Raymond D. Hettinger <python at rcn.com>. -Copyright (c) 2004 Python Software Foundation. All rights reserved. - -""" - from __future__ import division -import operator -import itertools -import heapq -import bisect import random -__all__ = ['mean', 'median', 'nlargest', 'nsmallest', - 'product', 'select', 'stddev'] - -def mean(data): - """Compute the arithmetic mean of a data sample""" - try: - cnt = len(data) # do it the fast way if possible - except TypeError: - cum = 0.0 - for cnt, elem in enumerate(data): - cum += elem - cnt += 1 # enumerate counts from zero - else: - cum = sum(data, 0.0) - try: - return cum / cnt - except ZeroDivisionError: - raise ValueError('data must have at least one element') - -def variance(data, sample=True): - """Computes the variance of the dataset. +def describe(data, sample=True): + """Computes the size, mean, and variance of the dataset. If sample is True, computes the variance with an n-1 divisor to reflect the additional degree of freedom in sampled data. If sample is False, @@ -80,25 +36,16 @@ m = newm if sample: try: - return (s / (k-1)) # sample variance + return (k, m, s / (k-1)) # sample variance except ZeroDivisionError: - raise ValueError('sample variance requires at least two elements') + return (k, m, None) else: - return (s / k) # population variance - -def stddev(data, sample=True): - """Computes the standard deviation of the dataset. - - If sample is True, computes the standard deviation with an n-1 divisor - for an unbiased estimator for a data sample. If sample is False, computes - with a divisor of n, giving the standard deviation for a complete population. - """ - return variance(data, sample) ** 0.5 + return (k, m, s / k, m) # population variance def select(data, n): """Find the nth rank ordered element (the least value has rank 0). - Equivalent to sorted(data)[n] + Equivalent to sorted(data)[n] but runs in O(n) time. """ data = list(data) if not 0 <= n < len(data): @@ -138,45 +85,12 @@ return select(data, n//2) return (select(data, n//2) + select(data, n//2-1)) / 2 -def product(iterable): - """Compute the product of the data elements""" - return reduce(operator.mul, iterable, 1) - -def nlargest(iterable, n=1): # XXX implement key= - """Find the n largest elements in the dataset""" - ## When collections.fibheap is available, use it instead of heapq - it = iter(iterable) - result = list(itertools.islice(it, n)) - heapq.heapify(result) - if len(result) == 0: - raise ValueError('data must have at least one element') - subst = heapq.heapreplace - sol = result[0] # sol --> Smallest of the nlargest - for elem in it: - if (elem > sol): - subst(result, elem) - sol = result[0] - result.sort(reverse=True) - return result - -def nsmallest(iterable, n=1): - """Find the n smallest elements in the dataset""" - ## When collections.fibheap is available, use it instead of bisect - it = iter(iterable) - result = sorted(itertools.islice(it, 0, n)) - if len(result) == 0: - raise ValueError('data must have at least one element') - insort = bisect.insort - pop = result.pop - los = result[-1] # los --> Largest of the nsmallest - for elem in it: - if (elem < los): - insort(result, elem) - pop() - los = result[-1] - return result - -XXX = """ - Other possible functions include data groupers for - binning, counting, and splitting into equivalence classes. -""" +if __name__ == '__main__': + print stats([3,4,5]) == (3, 4.0, 1.0) + print stats([2,4,6]) == (3, 4.0, 4.0) + print stats([3,5,7]) == (3, 5.0, 4.0) + from decimal import Decimal + print stats(map(Decimal,[3,5,7])) == (3, Decimal("5"), Decimal("4")) + print median(map(Decimal,[3,5,7])) == Decimal('5') + print median(map(Decimal,[3,5,7,9])) == Decimal('6') + print median(map(Decimal,[3,6,7,9])) == Decimal('6.5') Index: test_statistics.py =================================================================== RCS file: /cvsroot/python/python/nondist/sandbox/statistics/test_statistics.py,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- test_statistics.py 29 Feb 2004 09:12:28 -0000 1.3 +++ test_statistics.py 5 Sep 2004 11:42:36 -0000 1.4 @@ -1,5 +1,5 @@ from __future__ import division -from statistics import mean, stddev, product, nlargest, nsmallest, select, median +from statistics import describe, select, median ### UNITTESTS ################################################# @@ -15,6 +15,8 @@ class TestStats(unittest.TestCase): def test_mean(self): + def mean(data): + return describe(data)[1] self.assertEqual(mean(range(6)), 15/6.0) self.assertEqual(mean(g(6)), 15/6.0) self.assertEqual(mean([10]), 10) @@ -22,11 +24,14 @@ self.assertRaises(TypeError, mean, 'abc') def test_stddev(self): + def stddev(data, *args): + var = describe(data, *args)[2] + return var and var ** 0.5 or var self.assertEqual(stddev([10,15,20]), 5) self.assertEqual(round(stddev([11.1,4,9,13]), 3), 3.878) self.assertEqual(round(stddev([11.1,4,9,13], False), 3), 3.358) self.assertEqual(stddev([10], False), 0.0) - self.assertRaises(ValueError, stddev, [1]) + self.assertEqual(stddev([1]), None) self.assertRaises(ValueError, stddev, [], False) def test_median(self): @@ -42,13 +47,6 @@ self.assertEqual(median([30,10,2]), 10) self.assertRaises(ValueError, median, []) - def test_product(self): - self.assertEqual(product(range(1, 5)), 24) - def g(lo, hi): - for i in xrange(lo, hi): - yield i - self.assertEqual(product(g(1, 5)), 24) - def test_select(self): testdata = range(2000) + [20] * 500 + [85] * 500 sorteddata = sorted(testdata) @@ -74,30 +72,6 @@ self.assertRaises(ValueError, select, a, n+1) - def test_nlargest(self): - n = 10 - data = range(n) - random.shuffle(data) - copy = data[:] - self.assertEqual(nlargest(data), [n-1]) - self.assertRaises(ValueError, nlargest, data, 0) - for i in xrange(1, n+1): - self.assertEqual(nlargest(data, i), range(n-1, n-i-1, -1)) - self.assertEqual(data, copy) - self.assertEqual(nlargest('abcde', 3), list('edc')) - - def test_nsmallest(self): - n = 10 - data = range(n) - random.shuffle(data) - copy = data[:] - self.assertEqual(nsmallest(data), [0]) - self.assertRaises(ValueError, nlargest, data, 0) - for i in xrange(1, n+1): - self.assertEqual(nsmallest(data, i), range(i)) - self.assertEqual(data, copy) - self.assertEqual(nsmallest('abcde', 3), list('abc')) - if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestStats))
participants (1)
-
rhettingerīŧ users.sourceforge.net