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

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

  1. Yash Ganthe

    Yash Ganthe Guest

    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
    #1
    1. Advertising

  2. Yash Ganthe

    John Machin Guest

    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
    #2
    1. Advertising

  3. Yash Ganthe

    Robert Kern Guest

    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
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, May 22, 2009
    #3
  4. Yash Ganthe

    Yash Ganthe Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    4
    Views:
    427
  2. Roedy Green

    Shrinking icons

    Roedy Green, Jan 29, 2006, in forum: Java
    Replies:
    3
    Views:
    471
    Thomas Weidenfeller
    Jan 30, 2006
  3. Jerry Mcnealy
    Replies:
    0
    Views:
    326
    Jerry Mcnealy
    Aug 14, 2007
  4. Jerry Mcnealy
    Replies:
    0
    Views:
    404
    Jerry Mcnealy
    Aug 14, 2007
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,321
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page