PollPenn
PollPenn

Reputation: 733

How to change the values of a randomly selected subset of an array?

I have a numpy array containing a sequence of 1 and -1 of shape (1000,). I need to add some random noise to 10% of this sample. The noise will be a simple switch of the the sign of a given value (e.g. -1 if originally 1). My current solution is as follows:

# create an array of the indices of the values we are going to change
noisy_index = np.random.choice(1000, size=100, replace=False)

# multiply by -1 to change sign of these randomly selected values
mult = np.multiply(correct_labels[noisy_index], -1)

# delete these values from the original dataset
labels_subset = np.delete(correct_labels, noisy_index)

# concatenate the modified values to get back complete (and now noisy) dataset
correct_labels_noisy = np.concatenate((labels_subset, mult), axis=0)

Two questions: 1) is this doing what I described it should be doing? 2) is there a more straightforward and less verbose approach?

Upvotes: 1

Views: 36

Answers (1)

akuiper
akuiper

Reputation: 214957

The easiest would be multiply the selected values by -1 and assign back to the corresponding indices with:

correct_labels[noisy_index] *= -1

Example:

correct_labels = np.random.choice([1,-1], size=10)
print('original labels: ', correct_labels)
​
noisy_index = np.random.choice(10, size=3, replace=False)
correct_labels[noisy_index] *= -1
print('modified labels: ', correct_labels)

#original labels:  [ 1 -1 -1  1 -1 -1 -1 -1 -1  1]
#modified labels:  [ 1 -1 -1  1  1 -1  1 -1 -1 -1]
#                                ^     ^        ^

print('noisy index: ', noisy_index)
# noisy index:  [6 4 9]

Upvotes: 2

Related Questions