dimanche 17 juillet 2016

Numpy - check if elements of a array belong to another array

I have 2 numpy arrays:

xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
y = np.array([1.1,1.2])

I want to check whether each element of xarr belongs to y or equals 1.3. If an element belongs to y, return "y", if an element equals 1.3, return "y1", otherwise return "n"

I tried this:

x = np.where(xarr in y,"y",np.where(xarr == 1.3,"y1","n"))

but I got the wrong result, the first 2 elements should be "y" instead of "n"

['n' 'n' 'y1' 'n' 'n']

Don't know what I did wrong. Really appreciate any help

Aucun commentaire:

Enregistrer un commentaire