Comparing and Filtering NumPy array
Last Updated :
22 Feb, 2023
In this article, we are going to see how to perform a comparison and filtering of the NumPy array.
Comparing NumPy Array:
Let's see the comparison operators that will be used in comparing NumPy Arrays -
- Greater than (>) Or numpy.greater().
- Less Than (<) numpy.less().
- Equal(==) or numpy.equal()
- Not Equal(!=) or numpy.not_equal().
- Greater than and equal to(>=).
- Less than Equal to(<=).
Steps for NumPy Array Comparison:
Step 1: First install NumPy in your system or Environment. By using the following command.
pip install numpy(command prompt)
!pip install numpy(jupyter)
Step 2: Import NumPy module.
import numpy as np
Step 3: Create an array of elements using NumPy Array method.
np.array([elements])
Step 4: Now use comparison operators for comparing NumPy Array.
Example 1:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using greater() method.
Python3
# importing NumPy Module
import numpy as np
# Creating Array
a = np.array([1,2,3,4])
b = np.array([3,8,5,6])
# Comparing two arrays
np.greater(a, b)
Output:
array([False, False, False, False])
Time complexity: O(n), where n is the length of the arrays a and b.
Auxiliary space: O(n), where n is the length of the arrays a and b, since we are creating two arrays of size n to store the inputs.
Example 2:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using less() method.
Python3
# Importing NumPy Module
import numpy as np
# Creating Array using NumPy
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
np.less(a, b)
Output:
array([ True, True, True, True])
Example 3:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using equal() method.
Python3
# Importing NumPy Module.
import numpy as np
# Create Arrays using np.array() Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
# Compare a and b array elements
# if the elements in a and b are equal
# it returns True else returns False.
np.equal(a, b)
Output:
array([ False, False, False, False])
Example 4:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using not_equal() method.
Python3
# Importing NumPy Module.
import numpy as np
# Create Arrays using np.array() Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
# Compare a and b array elements if the
# elements in a and b are not equal
# it returns True else returns False.
np.not_equal(a, b)
Output:
array([ True, True, True, True])
Example 5:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using >= operator.
Python3
# Importing NumPy Module.
import numpy as np
# Create Arrays using np.array()
# Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
# it returns if elements in a are
# greater than a equal to b
print(a >= b)
Output:
[False False False False]
Example 6:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using <= operator.
Python3
# Importing NumPy Module.
import numpy as np
# Create Arrays using np.array()
# Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
# it returns if elements in a are less
# than a equal to b
print(a <= b)
Output:
[ True True True True]
Filtering NumPy Arrays:
Filtering means taking the elements which satisfy the condition given by us. For example, Even elements in an array, elements greater than 10 in an array, etc.Â
Steps for Filtering NumPy Array's:
- Import NumPy module.
- Create arrays using np.array() function.
- Write any condition for filtering the array.
- Create a new array with that filtering function.
Note: In Filtering and Comparison both give boolean values as an output.
Example 1:
- Import NumPy module.
- Create array using numpy.array() method.
- Now take a condition for filtering array.
- Now create a new array that satisfies the condition.
Python3
import numpy as np
a = np.array([1, 2, 3, 40, 50, 100,
45, 87, 98])
# Taking a condition to filter the array
filter_ex = a < 16
# Creating new array using Condition.
new_arr = np.array([filter_ex])
# Printing new Array
print(*new_arr)
Output:
[False False False True True True True True True]
Example 2:
- Import NumPy module.
- Create array using numpy.array() method.
- Now take a condition for filtering array.
- Now create a new array that satisfies the condition.
Python3
# Importing NumPy Module
import numpy as np
# Creating Array
a = np.array([1, 2, 3, 40, 50, 100,
45, 87, 98])
# Filtering Condition
filter2 = a % 2 == 0
even = np.array([filter2])
print(*even)
Output:
[False True False True True True False False True]
Similar Reads
How to compare two NumPy arrays? Here we will be focusing on the comparison done using NumPy on arrays. Comparing two NumPy arrays determines whether they are equivalent by checking if every element at each corresponding index is the same. Method 1: We generally use the == operator to compare two NumPy arrays to generate a new arr
2 min read
Boolean Array in NumPy - Python The goal here is to work with Boolean arrays in NumPy, which contain only True or False values. Boolean arrays are commonly used for conditional operations, masking and filtering elements based on specific criteria. For example, given a NumPy array [1, 0, 1, 0, 1], we can create a Boolean array wher
3 min read
Python | Numpy numpy.ndarray.__ne__() With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=
1 min read
Searching in a NumPy array Numpy provides various methods for searching different kinds of numerical values, in this article, we will cover two important ones. numpy.where()numpy.searchsorted() 1. numpy.where:() It returns the indices of elements in an input array where the given condition is satisfied. Syntax: numpy.where(co
3 min read
numpy.array_equal() in Python numpy.array_equal(arr1, arr2) : This logical function that checks if two arrays have the same shape and elements. Parameters : arr1 : [array_like]Input array or object whose elements, we need to test. arr2 : [array_like]Input array or object whose elements, we need to test. Return : True, if both ar
1 min read
Python | Numpy MaskedArray.__ne__ numpy.ma.MaskedArray class is a subclass of ndarray designed to manipulate numerical arrays with missing data. With the help of Numpy MaskedArray.__ne__ operator we can find that which element in an array is not equal to the value which is provided in the parameter. Syntax: numpy.MaskedArray.__ne__
1 min read
numpy.array_equiv() in Python numpy.array_equiv(arr1, arr2) : This logical function that checks if two arrays have the same elements and shape consistent. Shape consistent means either they are having the same shape, or one input array can be broadcasted to create the same shape as the other one. Parameters : arr1 : [array_like]
2 min read
How to filter two-dimensional NumPy array based on condition ? In this article, we are going to see how to apply the filter by the given condition in NumPy two-dimensional array. We have to obtain the output of required elements i.e., whatever we want to filter the elements from the existing array or new array. Here we are going to create a two-dimensional arra
4 min read
Intersection of two Arrays in Python ( Lambda expression and filter function ) Finding the intersection of two arrays using a lambda expression and the filter() function means filtering elements from one array that exist in the other. The lambda defines the condition (x in array2), and filter() applies it to the first array to extract common elements.For example, consider two
1 min read
Find common values between two NumPy arrays In this article, we are going to discuss how to find out the common values between 2 arrays. To find the common values, we can use the numpy.intersect1d(), which will do the intersection operation and return the common values between the 2 arrays in sorted order. Syntax: numpy.intersect1d(arr1, arr2
2 min read