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

Y

Yash Ganthe

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
 
J

John Machin

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?
 
R

Robert Kern

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
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
Y

Yash Ganthe

Thanks John,

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

-Yash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top