Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > numpy: frequencies

Reply
Thread Tools

numpy: frequencies

 
 
robert
Guest
Posts: n/a
 
      11-18-2006
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
 
Reply With Quote
 
 
 
 
Filip Wasilewski
Guest
Posts: n/a
 
      11-18-2006
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

 
Reply With Quote
 
 
 
 
Tim Hochberg
Guest
Posts: n/a
 
      11-18-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Histogram of character frequencies rajash@thisisnotmyrealemail.com C Programming 44 12-11-2007 05:31 AM
Getting word frequencies from files which are in folder. krisbee1983@gmail.com Python 5 04-05-2007 04:37 AM
Arbitrary Clock Frequencies From Base Clock abhisheknag@gmail.com VHDL 5 06-23-2006 12:45 PM
B vs G: Frequencies Used? (PeteCresswell) Wireless Networking 2 01-01-2006 08:16 PM
Cordless keyboard mouse frequencies Melv Computer Support 7 11-29-2003 03:34 AM



Advertisments