NumPy
stands as a cornerstone library in Python. One of the many useful functions it offers is numpy.isin
. This function allows you to test whether each element of an array is also present in a second array. It returns a boolean array of the same shape as the first array, indicating whether each element is found in the second array. This blog post will delve into the fundamental concepts, usage methods, common practices, and best practices of numpy.isin
.numpy.isin
?numpy.isin(element, test_elements, assume_unique=False, invert=False)
is a function that tests whether each element of element
is in test_elements
.
element
: The input array in which we want to check the presence of elements.test_elements
: The array against which we are comparing the elements of element
.assume_unique
: If set to True
, it assumes that both element
and test_elements
are unique, which can speed up the computation. The default is False
.invert
: If set to True
, the result is inverted, i.e., it returns True
for elements that are not in test_elements
. The default is False
.The function compares each element of element
with every element of test_elements
. If an element from element
is found in test_elements
, the corresponding position in the output boolean array is set to True
; otherwise, it is set to False
.
import numpy as np
# Create an input array
element = np.array([1, 2, 3, 4, 5])
# Create a test array
test_elements = np.array([3, 4, 5])
# Use numpy.isin
result = np.isin(element, test_elements)
print(result)
In this example, the output will be [False, False, True, True, True]
because only the elements 3, 4, and 5 from the element
array are present in the test_elements
array.
assume_unique
import numpy as np
element = np.array([1, 2, 3])
test_elements = np.array([2, 3])
# Assume both arrays are unique
result = np.isin(element, test_elements, assume_unique=True)
print(result)
When assume_unique
is set to True
, the function can use a more efficient algorithm for the comparison.
invert
import numpy as np
element = np.array([1, 2, 3, 4, 5])
test_elements = np.array([3, 4, 5])
# Invert the result
result = np.isin(element, test_elements, invert=True)
print(result)
The output will be [True, True, False, False, False]
because now it shows the elements that are not in the test_elements
array.
import numpy as np
data = np.array([10, 20, 30, 40, 50])
allowed_values = np.array([20, 40])
# Filter the data array
filtered_data = data[np.isin(data, allowed_values)]
print(filtered_data)
Here, we use numpy.isin
to create a boolean mask, which we then use to filter the data
array. The output will be [20, 40]
.
import numpy as np
# Create a 2D array
element_2d = np.array([[1, 2], [3, 4]])
test_elements_2d = np.array([2, 4])
result_2d = np.isin(element_2d, test_elements_2d)
print(result_2d)
The output will be a 2D boolean array [[False, True], [False, True]]
.
assume_unique
when appropriate: If you know that both arrays are unique, setting assume_unique=True
can significantly speed up the computation, especially for large arrays.element
, test_elements
, and filtered_data
makes the code more understandable.numpy.isin
call can help other developers (or your future self) understand the code.numpy.isin
is a powerful and versatile function in the NumPy library. It provides an easy way to test the presence of elements in an array, which is useful for filtering data, data cleaning, and many other data analysis tasks. By understanding its fundamental concepts, usage methods, common practices, and best practices, you can use numpy.isin
more efficiently and effectively in your projects.