numpy: frequencies

Discussion in 'Python' started by robert, Nov 18, 2006.

  1. robert

    robert Guest

    I have an integer array with values limited to range(a,b) like:

    ia=array([1,2,3,3,3,4,...2,0,1])

    and want to speedly count the frequencies of the integers into get a density matrix.
    Is this possible without looping?


    Question 2: is it possible to compute a "moving maximum" without python looping

    ia=array([4,2,1,5,3,2,2,0,1,1])
    -> mvmax(ia,3) ->
    [4,4,4,5,5,5,3,2,2,1])


    Robert
     
    robert, Nov 18, 2006
    #1
    1. Advertisements

  2. robert wrote:
    > I have an integer array with values limited to range(a,b) like:
    >
    > ia=array([1,2,3,3,3,4,...2,0,1])
    >
    > and want to speedly count the frequencies of the integers into get a density matrix.
    > Is this possible without looping?


    See numpy.bincount (for integers >= 0) if you mean 'without writing
    looping code in Python' or please specify your question.

    > Question 2: is it possible to compute a "moving maximum" without python looping
    >
    > ia=array([4,2,1,5,3,2,2,0,1,1])
    > -> mvmax(ia,3) ->
    > [4,4,4,5,5,5,3,2,2,1])


    I haven't seen a ready solution but this can be easily converted into
    Pyrex/C looping.

    cheers,
    fw
     
    Filip Wasilewski, Nov 18, 2006
    #2
    1. Advertisements

  3. robert

    Tim Hochberg Guest

    Filip Wasilewski wrote:
    > robert wrote:
    >> I have an integer array with values limited to range(a,b) like:
    >>
    >> ia=array([1,2,3,3,3,4,...2,0,1])
    >>
    >> and want to speedly count the frequencies of the integers into get a density matrix.
    >> Is this possible without looping?

    >
    > See numpy.bincount (for integers >= 0) if you mean 'without writing
    > looping code in Python' or please specify your question.
    >
    >> Question 2: is it possible to compute a "moving maximum" without python looping
    >>
    >> ia=array([4,2,1,5,3,2,2,0,1,1])
    >> -> mvmax(ia,3) ->
    >> [4,4,4,5,5,5,3,2,2,1])

    >
    > I haven't seen a ready solution but this can be easily converted into
    > Pyrex/C looping.


    I don't know a way to avoid looping entirely, but there are ways that
    you can loop over the width of the window (in this case 3) rather than
    over the entire array. Since the window width is generally small
    compared to the array, this will probably be fast enough. The tricky
    part is to get the value right at the edges, since what you do there
    depends on what boundary conditions you apply.

    The general idea is this:

    result = ia[n-1:]
    for i in range(n-1):
    numpy.maximum(result, ia[i:-n+i], result)

    This punts on dealing with the ends (and I haven't tested this version),
    but should give you the idea.

    -tim
     
    Tim Hochberg, Nov 18, 2006
    #3
    1. Advertisements

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. Replies:
    5
    Views:
    2,653
    Ricardo
    Jun 23, 2006
  2. drife
    Replies:
    1
    Views:
    520
    Travis E. Oliphant
    Mar 1, 2006
  3. Replies:
    5
    Views:
    493
    Alex Martelli
    Apr 5, 2007
  4. Duncan Smith
    Replies:
    3
    Views:
    658
    Duncan Smith
    Apr 25, 2007
  5. Replies:
    2
    Views:
    658
    Robert Kern
    Nov 13, 2007
  6. Histogram of character frequencies

    , Dec 1, 2007, in forum: C Programming
    Replies:
    44
    Views:
    1,494
    Peter 'Shaggy' Haywood
    Dec 11, 2007
  7. W. eWatson
    Replies:
    2
    Views:
    1,326
    W. eWatson
    Nov 23, 2009
  8. Tom Kacvinsky

    Installing numpy over an older numpy

    Tom Kacvinsky, Jun 15, 2012, in forum: Python
    Replies:
    1
    Views:
    538
    Miki Tebeka
    Jun 15, 2012
Loading...