Question
Suppose I now have an array that looks like arr = np.random.randint(1, 4, 100), where the unique numbers are 1, 2, and 3.
Now I would like to model the data corruption, where some of the numbers are likely to become others. For example, if arr[k] is 1, then it is likely to remain the same, but it is also possible to become 2 or 3 (all with equal probabilities).
I could implement this using following code
import numpy as np
arr = np.random.randint(1, 4, 100)
mask = np.random.choice([0, 1], size=100, p=[0.8, 0.2])
for idx in range(100):
if mask[idx] != 0:
arr[idx] = np.random.choice([1, 2, 3)
This works fine but I really do not like the loop. Is there some way I could eliminate the (ugly) loop?