not_equal() Function - NumPy
The numpy.char.not_equal() function checks if two string arrays (or scalars) are not equal element-wise. It returns True for elements that differ and False for elements that are the same. This is useful for comparing arrays of text data.
For Example: This example compares two single words and checks if they are not equal.
import numpy as np
a = np.array('Hello')
b = np.array('World')
res = np.char.not_equal(a, b)
print(res)
Output
True
Explanation: Since "Hello" and "World" are different, the function returns True.
Syntax
numpy.char.not_equal(arr1, arr2)
Parameters:
- arr1: array_like (str/unicode) - First input string or array.
- arr2: array_like (str/unicode) - Second input string or array.
Return Value: ndarray / bool - Boolean array or single boolean value showing element-wise inequality.
Examples
Example 1: This example compares two identical strings.
import numpy as np
a = np.array('numpy')
b = np.array('numpy')
res = np.char.not_equal(a, b)
print(res)
Output
False
Explanation: Both arrays contain "numpy", so they are equal - result is False.
Example 2: This example compares two arrays of words.
import numpy as np
a = np.array(['Geeks', 'for', 'Geeks'])
b = np.array(['Geek', 'for', 'Geek'])
res = np.char.not_equal(a, b)
print(res)
Output
[ True False True]
Explanation:
- "Geeks" vs "Geek" -> not equal -> True
- "for" vs "for" -> equal -> False
- "Geeks" vs "Geek" -> not equal -> True
Example 3: This example checks inequality in numeric strings.
import numpy as np
a = np.array(['10', '11', '12'])
b = np.array(['10', '11', '121'])
res = np.char.not_equal(a, b)
print(res)
Output
[False False True]
Explanation: Only the last pair "12" vs "121" differs, so result is True for that position.