[Numpy-discussion] fancy indexing/broadcasting question
Mark.Miller
mpmusu at cc.usu.edu
Sat Jul 7 14:37:59 EDT 2007
A quick question for the group. I'm working with some code to generate
some arrays of random numbers. The random numbers, however, need to
meet certain criteria. So for the moment, I have things that look like
this (code is just an abstraction):
import numpy
normal=numpy.random.normal
RNDarray = normal(25,15,(50,50))
tmp1 = (RNDarray < 0) | (RNDarray > 25)
while tmp1.any():
print tmp1.size, tmp1.shape, tmp1[tmp1].size
RNDarray[tmp1] = normal(5,3, size = RNDarray[tmp1].size)
tmp1 = (RNDarray < 0) | (RNDarray > 25)
This code works well. However, it might be considered inefficient
because, for each iteration of the while loop, all values get
reevaluated even if they have previously met the criteria encapsulated
in tmp1. It would be better if, for each cycle of the while loop, only
those elements that have previously not met criteria get reevaluated.
I tried a few things that haven't worked. An example is provided below
(changed code marked with *):
RNDarray = normal(25,15,(50,50))
tmp1 = (RNDarray < 0) | (RNDarray > 25)
while tmp1.any():
print tmp1.size, tmp1.shape, tmp1[tmp1].size
RNDarray[tmp1] = normal(5,3, size = RNDarray[tmp1].size)
* tmp1 = (RNDarray[tmp1] < 0) | (RNDarray[tmp1] > 25)
I see why this doesn't work properly. When tmp1 is reevaluated in the
while loop, it returns an array with a different shape.
Does anyone have any suggestions for improvement here? Please let me
know if any of this requires clarification.
Thanks,
-Mark
More information about the NumPy-Discussion
mailing list