Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

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

like image 952
Square9627 Avatar asked Jun 20 '16 02:06

Square9627


People also ask

How do you compare elements of two NumPy arrays?

To check if two NumPy arrays A and B are equal: Use a comparison operator (==) to form a comparison array. Check if all the elements in the comparison array are True.

How do you check if an element is in an array NumPy?

Using Numpy array, we can easily find whether specific values are present or not. For this purpose, we use the “in” operator. “in” operator is used to check whether certain element and values are present in a given sequence and hence return Boolean values 'True” and “False“.

What does * do in NumPy?

NumPy performs operations element-by-element, so multiplying 2D arrays with * is not a matrix multiplication – it's an element-by-element multiplication.

How do you check if an array is in a list of arrays?

The variable is_in_list indicates if there is any array within he list of numpy arrays which is equal to the array to check. Show activity on this post. You are assuming that all arrays are of the same shape, which is not clear from the question. Then you convert the whole list to an array.


2 Answers

You can make use of numpy.in1d, the rest is pretty simple:

The key part:

In [25]: np.in1d(xarr, y)
Out[25]: array([ True,  True, False, False, False], dtype=bool)

Whole example:

In [16]: result = np.empty(len(xarr), dtype=object)

In [17]: result
Out[17]: array([None, None, None, None, None], dtype=object)

In [18]: result.fill("n")

In [19]: result
Out[19]: array(['n', 'n', 'n', 'n', 'n'], dtype=object)

In [20]: result[np.in1d(xarr, y)] = 'y'

In [21]: result
Out[21]: array(['y', 'y', 'n', 'n', 'n'], dtype=object)

In [23]: result[xarr == 1.3] = 'y1'

In [24]: result
Out[24]: array(['y', 'y', 'y1', 'n', 'n'], dtype=object)

Edit:

A small modification of your original attempt:

In [16]: x = np.where(np.in1d(xarr, y),"y",np.where(xarr == 1.3,"y1","n"))

In [17]: x
Out[17]: 
array(['y', 'y', 'y1', 'n', 'n'], 
      dtype='|S2')

The problem in your original attempt was that xarr in y gives just False.

like image 106
Akavall Avatar answered Sep 30 '22 00:09

Akavall


Check np.isin().

isin is an element-wise function version of the python keyword in.

isin(a, b) is roughly equivalent to np.array([item in b for item in a]) if a and b are 1-D sequences.

like image 33
Nirmal Avatar answered Sep 30 '22 00:09

Nirmal