Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I random 0 or 1 and replace values in Python array?

For my coding:

In[1]:
TC = [[0, 0, 0, 0, 0, 2000, 2000, 2000, 2000, 0, 0, 0], 
      [0, 1000, 1000, 1000, 1000, 0, 0, 0, 0, 0, 0, 0]]
a = np.array(TC)
arr = a.reshape(2,12) 
arr

Out[1]:
array([[   0,    0,    0,    0,    0, 2000, 2000, 2000, 2000,    0,    0,
           0],
       [   0, 1000, 1000, 1000, 1000,    0,    0,    0,    0,    0,    0,
           0]])

In[2]:
array_new=[]
for i in range (2):
    for j in range (12):
        rand=np.random.choice([0,1])
        y = np.select([arr[i][j] > 0], [rand], 999)
        array_new.append(y)
    j=j+1
i=i+1
numpy_array = np.array(array_new)
array_new = numpy_array.reshape(2,12) 
array_new

Out[2]:
array([[999, 999, 999, 999, 999,   0,   1,   0,   1, 999, 999, 999],
       [999,   1,   1,   1,   1, 999, 999, 999, 999, 999, 999, 999]])

Does any way to make each value in each row that more than zero such as 2000 1000 in TC array to be *random values, consisting three 0 and one 1, then replace it each row?

Target
Out[2]:
array([[999, 999, 999, 999, 999,   *0,   *0,   *0,   *1, 999, 999, 999],  
       [999,   *1,   *0,   *0,   *0, 999, 999, 999, 999, 999, 999, 999]])

Thank You in Advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Given an array:

arr = np.array([[0, 0, 0, 0, 0, 2000, 2000, 2000, 2000, 0, 0, 0], 
      [0, 1000, 1000, 1000, 1000, 0, 0, 0, 0, 0, 0, 0]])

It looks like you want to randomly replace four values in each row with three 0s and one 1. To do that, you’ll first need to select the four values you are going to replace:

gt_zero = arr > 0
eq_zero = arr == 0

This creates two boolean arrays of the same shape as arr that have True where the condition is true, and False where the condition is false

Now, since you know that each row is guaranteed to have four non-zero values, you can pre-build a Nx4 array of replacement values. First, create an array of zeros. Then, select a random column to insert 1 into for each row. Then, replace the element in that column with 1 using np.put_along_axis

replacements = np.zeros((arr.shape[0], 4))
random_column = np.random.randint(0, 4, (arr.shape[0], 1))
np.put_along_axis(replacements, random_column, 1, 1)

At the end of this snippet, replacements looks like this:

array([[0., 1., 0., 0.],
       [0., 0., 0., 1.]])

Finally, replace the elements in arr with replacements. Note that you need to flatten replacements to be able to set the values using boolean indexing:

arr[gt_zero] = replacements.flatten()
arr[eq_zero] = 999

Which gives you the following (example) arr:

array([[999, 999, 999, 999, 999,   0,   1,   0,   0, 999, 999, 999],
       [999,   0,   0,   0,   1, 999, 999, 999, 999, 999, 999, 999]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading