0

I have two arrays:

x=np.array([0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,1])
y=np.array([y0,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12])

Where the elements y_i correspond only to the 0-elements in the array x.

I want to modify one random 0-element in x and to make it 1. At this time, I have to delete the corresponding element in y (because y contains only element related to 0 in the array x).

[EDIT:] for example, I have

x=np.array([0,0,0,0,0,1,1,0,0,1,0]
 y=np.array([21,32,45,54,87,90,88,60])

such that to x[0] corresponds y[0]=21, x[1]—> y[1]=32, x[2]—>y[2]=45, x[3]—> y[3]=54, x[4]—>y[4]=87, x[7]—>y[5]=90, x[8]—>y[6]=88, x[10]—>y[7]=60. If I change x[2], x[7] and x[10] from 0 to 1, I want to cancel the corresponding elements in y, i.e. 45, 90 and 60

0

1 Answer 1

1

This may be what you are looking for:

x_0 = np.where(x==0)[0]

rand_ind = np.random.randint(1,len(x_0))

x[x_0[rand_ind]] = 1;

y = np.delete(y, rand_ind)

x_0 is an array of elements in x initially equal to 0. rand_ind is a random index chosen from x_0 length range. To change 0 to 1 the program uses x_0[rand_ind] while to delete the corresponding element in y it simply uses rand_ind.


This is a modified solution for the updated question,

import numpy as np
import random

x_0 = np.where(x==0)[0]

rand_ind = np.array(random.sample(list(x_0), 3))

x[rand_ind] = 1

y = np.delete(y, [np.where(x_0==i)[0] for i in rand_ind])

In this solution rand_ind is a direct choice of indices of the x array and not the trimmed x_0 array. It chooses the indices stored in x_0 to avoid choosing indexes where the value in x is 1. The indexes i.e. elements of x_0 are the indexes of x while the indexes of x_0 correspond to indexes/positions in y.

Program uses random.sample as a straightforward way to pick N elements at random without repetition (here 3). It then turns the elements at rand_ind in x to 1 and deletes the corresponding elements in y. The last step is done by using a generator and finding the indices of x_0 elements with each rand_ind value.

Sign up to request clarification or add additional context in comments.

10 Comments

Glad to help :) The best way is probably to simply generate rand_ind as an array, randint has that option.
Thanks! But I have another question about it. If I change the value in the 2nd and 7th position in array x, the program you wrote doesn't delete the correspondent element in y... I tried to put your code in python with this specific example but it doesn't work properly...
I mean, I have x=[0,0,0,0,0,1,0,0] and y=[21,45,32,98,12,5,3], for instance. I want the 2nd and 6th element from 0 to 1. So your program does it. Since the elements in y are related only to null x elements, there won’t be correspondence in indices between x and y, right? In this way, I want to delete the elements 32 and 5 because they correspond to x-elements I changed. It seems that your program doesn’t delete the right elements from y, no? Thanks for your helpful discussion!
It was right as I wrote before! Try to explain better: x=np.array([0,0,0,0,0,1,0,0]) and y=np.array([21,45,32,98,12,5,3]). So, to x[0] corresponds 21=y[0], x[1]->45=y[1], x[2]->32=y[2], x[3]->98=y[3], x[4]->12=y[4], x[6]->5=y[5], x[7]->3=y[6]. The indices of y-elements is different at a certain point from those of x-elements. This happens because in x there's an element =1 (that is x[5], that isn't related to any elements in y). If I change x[2] and x[6], how can I remove the corresponding elements in y, i.e. y[2]=32 and y[5]=5? I know that it's an insanity, but I need to do it in this way...
If you don’t have any solution Maybe it’s better to post it in another thread to have more visibility... thanks for this discussion :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.