Skip to content Skip to sidebar Skip to footer

How To Change Values In Numpy Array

import numpy as np a=np.array([[4,2,6],[3,6,5]]) b=np.array([3,5]) I want to update the numbers in 'a' which are bigger than the numbers in 'b' to np.nan. If they are smaller or e

Solution 1:

You can write so:

import numpy as np
a=np.array([[4,2,6],[3,6,5]])
b=np.array([3,5])

# shape in compared axis must be the same or one of their length must be equal 1# in this case their shape is b(2,1) and a(2,3)

a = np.where(a <= b.reshape(b.shape[0],1), a, np.nan)
print(a)

but in more difficult cases I'm not sure, that it will work

Post a Comment for "How To Change Values In Numpy Array"