By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

numpy: frequencies

P: n/a
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
Nov 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Nov 18 '06 #2

P: n/a
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

Nov 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.