I'm reading in an array of numbers arr
from a csv file (1000+ entries). I figured out a way to replace a user-specified fraction of these numbers err_frac
with random values drawn from a uniform distribution. For example:
err_frac = 0.2
swap_count = math.floor(err_frac * len(arr))
swap = np.random.uniform(low=np.amin(arr), high=np.amax(arr), size=swap_count)
arr.flat[np.random.choice(len(arr), swap_count, replace=False)] = swap
Now I'd like to take a randomly chosen fraction of these numbers and add or subtract some user-specified percentage of its original value. For example, if both percentages were 20%, the script would:
(a) randomly select 20% of the numbers in the original array
(b) if the chosen number was greater than the mean of the dataset, subtract 20% of its original value
(c) if the chosen number was smaller than the mean of the dataset, add 20% of its original value
(d) rewrite the modified values to a new array
Seems easy enough but I'm running into a mental block here. I tried something like this, but it's failing miserably:
for nz in range(1, len(arr)):
nzc = nz - 1
if arr[nzc] > 0.5 * np.amax(arr):
swap = arr[nzc] - err_frac * arr[nzc]
arr.flat[np.random.choice(len(arr), math.floor(arr[nzc]), replace=False)] = swap
elif arr[nzc] < 0.5 * np.amax(arr):
swap = arr[nzc] + err_frac * arr[nzc]
arr.flat[np.random.choice(len(arr), math.floor(arr[nzc]), replace=False)] = swap
Ideas?
Aucun commentaire:
Enregistrer un commentaire