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

Discussion in 'Python' started by Yash Ganthe, May 22, 2009.

1. ### Yash GantheGuest

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 or any other module have functions for achieving
this?

Which function can be used for doing this?

Thanks,
Yash

Yash Ganthe, May 22, 2009

2. ### John MachinGuest

On May 22, 8:03 pm, Yash Ganthe <> 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?

John Machin, May 22, 2009

3. ### Robert KernGuest

On 2009-05-22 08:50, Scott David Daniels wrote:
> Yash Ganthe wrote:
>> 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 this give you a clue?
>
> import numpy as np
>
> v = np.arange(128)
> v.shape = (16, 8)
> sum(v.transpose()) / 8.

Or even:

import numpy as np

v = np.arange(1000).reshape((-1, 10))
ten_point_mean = v.mean(axis=1)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

Robert Kern, May 22, 2009
4. ### Yash GantheGuest

Thanks John,

The code u provided works for me. Indeed it is a simple requirement
and I am a complete novice to Python.

-Yash

Yash Ganthe, May 23, 2009