numpy.invert() is a bitwise function in NumPy used to invert each bit of an integer array. It performs a bitwise NOT operation, flipping 0s to 1s and 1s to 0s in the binary representation of integers. Example:
Python
import numpy as np
a = np.array([1, 2, 3])
res = np.invert(a)
print(res)
Explanation: Input array a contains [1, 2, 3]. When np.invert(a) is applied, it flips the bits of each integer. For signed integers, ~x is equivalent to -(x + 1). So, ~1 = -2, ~2 = -3, and ~3 = -4.
Syntax
numpy.invert(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True, signature=None)
Parameter:
Parameter | Description |
---|
x | Input array containing integers (or booleans). |
---|
out | A location into which the result is stored (optional). |
---|
where | Condition over elements to apply the function (optional). |
---|
Returns: A new NumPy array with the bitwise NOT of the elements in x.
Examples
Example 1: In this example, we flip the values True becomes False and False becomes True. This is similar to applying a logical NOT operation.
Python
import numpy as np
a = np.array([True, False, True])
res = np.invert(a)
print(res)
Explanation: Input array a contains [True, False, True]. When np.invert(a) is applied, it flips each boolean value True becomes False and False becomes True.
Example 2: In this example, we are inverting an integer array using the out parameter to store the result.
Python
import numpy as np
arr = np.array([10, 20, 30])
inv = np.empty_like(arr)
np.invert(arr, out=inv)
print(inv)
Explanation: Array contains [10, 20, 30]. Here, we use the out parameter to store the result of np.invert() in inv. Since ~x = -(x + 1) for integers, the output is [-11, -21, -31], stored directly in the inv array.
Example 3: In this example, we conditionally apply numpy.invert() using the where parameter. We also pass an output array (out) to preserve values where the condition is False.
Python
import numpy as np
a = np.array([4, 5, 6])
arr = np.copy(a)
np.invert(a, out=arr, where=[True, False, True])
print(arr)
Explanation: Array a contains [4, 5, 6]. Using where, np.invert() is applied only where True and out stores the result. So, ~4 = -5, 5 stays unchanged and ~6 = -7.
Similar Reads
numpy.isnan() in Python The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array. Syntax :Â numpy.isnan(array [, out]) Parameters :Â array : [array_like]Input array or object whose elements, we need to test for infinity out : [ndarray, optional]Output array placed wit
2 min read
numpy.nonzero() in Python numpy.nonzero() function returns the indices of the elements in an array that are non-zero. It is commonly used to find the positions of non-zero (or True) elements in arrays.Example:Pythonimport numpy as np a = np.array([0, 2, 0, 3, 0, 4]) res = np.nonzero(a) print(res)Output(array([1, 3, 5]),) Exp
2 min read
numpy.isinf() in Python numpy.isinf() test element-wise whether a value is positive or negative infinity. It returns a Boolean array with True where the input is either +inf or -inf and False otherwise. Example:Pythonimport numpy as np a = np.array([1, np.inf, -np.inf, 0, np.nan]) res = np.isinf(a) print(res)Output[False T
2 min read
numpy.negative() in Python numpy.negative() function is used when we want to compute the negative of array elements. It returns element-wise negative value of an array or negative value of a scalar. Syntax : numpy.negative(arr, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, ext
2 min read
numpy.isneginf() in Python The numpy.isneginf() function tests element-wise whether it is negative infinity or not, and returns the result as a boolean array. Syntax :  numpy.isneginf(array, y = None) Parameters : array : [array_like]Input array or object whose elements, we need to test for infinity. y : [array_like]A boole
2 min read