Looking for module for shrinking a list with n-point means

John Machin sjmachin at lexicon.net
Fri May 22 07:00:19 EDT 2009


On May 22, 8:03 pm, Yash Ganthe <yas... at gmail.com> wrote:
> Hi,
>
> I would like to shrink a large list in the following way:
> If the List has 1000 integers, we need only 100 averages such that the
> 1000 points are average for every 10 consecutive values. So p0 to p9
> will be averaged to obtain t0. p10 to p19 will be averaged to obtain
> t1 and so on. This is a 10-point mean.
>
> We are doing this as we collect a lot of data and plot it on a graph.
> Too many samples makes the graph cluttered. So we need to reduce the
> number of values in the way described above.
>
> Does SciPy or NumPy

What do their docs say?


> or any other module have functions for achieving
> this?
>
> Which function can be used for doing this?

Perhaps one like this:

| >>> def n_point_means(alist, n):
| ...     blist = alist[:]
| ...     blist.sort()
| ...     size = len(blist)
| ...     assert 1 <= n <= size
| ...     assert size % n == 0
| ...     clist = []
| ...     fn = float(n)
| ...     for i in xrange(0, size, n):
| ...         clist.append(sum(blist[i:i+n]) / fn)
| ...     return clist
| ...
| >>> aaa = [9,8,7,6,5,4,3,2,0]
| >>> n_point_means(aaa,2)
| Traceback (most recent call last):
|   File "<stdin>", line 1, in <module>
|   File "<stdin>", line 6, in n_point_means
| AssertionError
| >>> aaa = [9,8,7,6,5,4,3,2,1,0]
| >>> n_point_means(aaa,2)
| [0.5, 2.5, 4.5, 6.5, 8.5]
| >>> n_point_means(aaa,5)
| [2.0, 7.0]
| >>>

Does that do what you want? If your requirement is so simple, why not
write it yourself?




More information about the Python-list mailing list