Home > Software engineering >  Change values in np array Python
Change values in np array Python

Time:02-14

I am trying to change the value in np array if there is a match

For example, a np array a and its shape is (50,4)

a.shape#(50,4)

I need to check if the np array a has this value [0,1,20,4] in the 1st axis, then I need to change that into [-1,-1,-1,-1].

I tried this format -

a[a==[0,1,20,4]]=[-1,-1,-1,-1]

But, this is not working,

how to do this modification, thanks

CodePudding user response:

Maybe try doing row-wise comparison with np.tile and np.argwhere like this:

import numpy as np

# Create dummy data
x = np.random.randint(29, size=(49, 4))
x = np.concatenate([[[0,1,20,4]], x])
print('Before --> \n', x)

# Compare row-wise
x[np.argwhere(np.all(x==np.reshape(np.tile([0,1,20,4], reps=x.shape[0]), x.shape),axis=1))] = [-1,-1,-1,-1]

print('After --> \n', x)
Before --> 
 [[ 0  1 20  4]
 [ 1 17  5  2]
 [19  8 24 17]
 [ 1 23 16  3]
 [ 0 15  0 20]
 [14 23  9 23]
 [ 1 27  5 27]
 [15 24 24 17]
 [ 2 28  8  4]
 [26 26  6 10]
 [18 13  5 28]
 [10 25 18 15]
 [ 6 17  8  2]
 [ 4 26 26 15]
 [18 16 18 24]
 [ 0 11 15 22]
 [20 27  0  0]
 [ 9 22 16  2]
 [22 11  8 23]
 [21 10  6 23]
 [14 16  0 10]
 [14 27 22  9]
 [ 4  0 10 15]
 [12  0 28 25]
 [ 8 28  9 28]
 [12  3 26 24]
 [23  3 26 25]
 [ 0  6 16  4]
 [ 1 20  1 19]
 [11  5  9 11]
 [20 15 18  5]
 [25  0 17 27]
 [20 24  3 19]
 [13 12 17  4]
 [ 1 13 25 22]
 [27 10 11 18]
 [ 4  5 12  8]
 [11 19 17 15]
 [26  7  3 10]
 [21 14 27 21]
 [26 12  8 13]
 [27  8  1 17]
 [20 28  0 20]
 [ 1 12 11 16]
 [ 4  0  3 22]
 [11 12  3  8]
 [15 24  3  8]
 [ 4 23 17 20]
 [21  1 23 12]
 [ 0 27  3 22]
 [15  5 17 28]]
After --> 
 [[-1 -1 -1 -1]
 [ 1 17  5  2]
 [19  8 24 17]
 [ 1 23 16  3]
 [ 0 15  0 20]
 [14 23  9 23]
 [ 1 27  5 27]
 [15 24 24 17]
 [ 2 28  8  4]
 [26 26  6 10]
 [18 13  5 28]
 [10 25 18 15]
 [ 6 17  8  2]
 [ 4 26 26 15]
 [18 16 18 24]
 [ 0 11 15 22]
 [20 27  0  0]
 [ 9 22 16  2]
 [22 11  8 23]
 [21 10  6 23]
 [14 16  0 10]
 [14 27 22  9]
 [ 4  0 10 15]
 [12  0 28 25]
 [ 8 28  9 28]
 [12  3 26 24]
 [23  3 26 25]
 [ 0  6 16  4]
 [ 1 20  1 19]
 [11  5  9 11]
 [20 15 18  5]
 [25  0 17 27]
 [20 24  3 19]
 [13 12 17  4]
 [ 1 13 25 22]
 [27 10 11 18]
 [ 4  5 12  8]
 [11 19 17 15]
 [26  7  3 10]
 [21 14 27 21]
 [26 12  8 13]
 [27  8  1 17]
 [20 28  0 20]
 [ 1 12 11 16]
 [ 4  0  3 22]
 [11 12  3  8]
 [15 24  3  8]
 [ 4 23 17 20]
 [21  1 23 12]
 [ 0 27  3 22]
 [15  5 17 28]]

CodePudding user response:

This solves it for random array of shape (50,4)

b = np.random.randint(5,size=(50,4))
c = np.equal(b,[4,1,2,3])
ai = np.array(c.all(axis=1).nonzero())
np.put_along_axis(b,ai,[-1,-1,-1,-1],axis=0)

change [4,1,2,3] to [0,1,20,4]

CodePudding user response:

For example for a random matrix

x = np.random.randint(1,11,size=(10,4))
>> array([[10,  1,  7,  7],
   [ 3,  1,  3,  2],
   [ 8, 10,  2,  9],
   [ 4,  6,  4,  4],
   [ 1,  4,  3,  5],
   [10,  1,  5,  4],
   [ 7, 10,  8,  8],
   [ 5, 10,  8,  3],
   [ 6,  5,  5,  3],
   [ 7,  2,  5,  7]])

Check all rows that are equal to target row

rows_cond = np.all(x == [1,4,3,5], axis=1)

this returns a boolean array that can be used to modify the desired rows

x[rows_cond,:]  = [-1,-1,-1,-1]
  • Related