Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

numpy - How to properly index to an array of changing size due to masking in python

This is a problem I've run into when developing something, and it's a hard question to phrase. So it's best with an simple example:

Imagine you have 4 random number generators which generate an array of size 4:

[rng-0,  rng-1,   rng-2,   rng-3]
   |        |        |        | 
[val0,    val1,    val2,    val3]

Our goal is to loop through "generations" of arrays populated by these RNGs, and iteratively mask out the RNG which outputted the maximum value.

So an example might be starting out with:
mask = [False, False, False, False], arr = [0, 10, 1, 3], and so we would mask out rng-1.

Then the next iteration could be: mask = [False, True, False, False], arr = [2, 1, 9] (before it gets asked, yes arr HAS to decrease in size with each rng that is masked out). In this case, it is clear that rng-3 should be masked out (e.g. mask[3] = True), but since arr is now of different size than mask, it is tricky to get the right indexing for setting the mask (since the max of arr is at index 2 of the arr, but the corresponding generator is index 3). This problem grows more an more difficult as more generators get masked out (in my case I'm dealing with a mask of size ~30).

If it helps, here is python version of the example:

rng = np.random.RandomState(42)
mask = np.zeros(10, dtype=bool)  # True if generator is being masked
for _ in range(mask.size):
    arr = rng.randint(100, size=~mask.sum())
    unadjusted_max_value_idx = arr.argmax()
    adjusted_max_value_idx = unadjusted_max_value_idx + ????
    mask[adjusted_max_value_idx] = True

Any idea a good way to map the index of the max value in the arr to the corresponding index in the mask? (i.e. moving from unadjusted_max_value_idx to adjusted_max_value_idx)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
#use a helper list
rng = np.random.RandomState(42)
mask = np.zeros(10, dtype=bool)  # True if generator is being masked
ndxLst=list(range(mask.size))
maskHistory=[]
for _ in range(mask.size):
    arr = rng.randint(100, size=(~mask).sum())
    unadjusted_max_value_idx = arr.argmax()
    adjusted_max_value_idx=ndxLst.pop(unadjusted_max_value_idx)
    mask[adjusted_max_value_idx] = True
    maskHistory.append(adjusted_max_value_idx)
print(maskHistory)
print(mask)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...