In Python, invert()
refers to a bitwise NOT operation that flips all bits of an integer, changing 1s to 0s and 0s to 1s.
Key takeaways:
numpy.invert()
is used to perform a bitwise NOT operation, flipping the bits of each element in an array.
It operates on integer arrays and inverts every bit: 1 becomes 0, and 0 becomes 1.
The result of the inversion depends on the binary representation, and negative numbers may appear due to two's complement representation.
This function is particularly useful for binary data processing, flag manipulation, and working with low-level data or hardware interfaces.
numpy.invert()
is efficient for large arrays and can be applied to elements independently while preserving the structure of the original array.
invert()
functionIn NumPy, the invert()
function is used to perform a bitwise NOT operation on an array. This operation flips the bits of each element in the array, meaning that each bit that is 1
becomes 0
, and each bit that is 0
becomes 1
. The result of the inversion is a new array with the bits of the original elements inverted.
This operation is particularly useful when working with binary representations or performing low-level bitwise manipulations.
numpy.invert()
The basic syntax of numpy.invert()
is:
numpy.invert(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'invert'>)
x
: The input array on which the bitwise NOT operation is performed. It can be an array of integers or boolean.
/
: The /
indicates that x
is a positional-only argument, meaning you must pass it without explicitly naming it.
out
(ndarray
, optional): A location where the result is stored. If not provided, a new array is created.
where
(array-like of bool, optional): A boolean array indicating where to perform the operation.
casting
(optional): Controls what kind of data casting is allowed. They can be:
'same_kind'
means only safe casts between types (e.g., integer to integer).
'no'
means no casting is allowed (throws an error if types differ).
'unsafe'
allows any kind of casting, even if it results in data loss.
'equiv'
means only equivalent types can be cast.
order
(optional): Specifies the memory layout of the output array.
dtype
(optional): Specifies the desired data type of the result.
subok
(optional): By default, this is set to True
. If set to False
, the result will always be a standard ndarray
, excluding any subtypes.
The function returns a new array where the bits of the original elements are inverted.
numpy.invert()
work?The numpy.invert()
function performs a bitwise NOT operation on the binary representation of the elements in the array. For example, consider an 8-bit binary representation of a number:
5
in binary is 00000101
.
Applying invert()
on 5
will result in 11111010
, which is -6
in decimal (because of two’s complement representation of negative numbers in binary).
numpy.invert()
Let’s explore different examples of using numpy.invert()
function.
In this example, we apply numpy.invert()
to a small array of integers.
import numpy as nparr = np.array([1, 2, 3, 4], dtype=np.int16)result = np.invert(arr,casting='safe',order='C',dtype=np.int32,subok=False)print("Original array:", arr)print("Original dtype:", arr.dtype)print("Result array:", result)print("Result dtype:", result.dtype)
Line 1: We import numpy
as np
to use its functionalities.
Line 3: We initialize an array named arr
with integers [1, 2, 3, 4]
and specify its data type as int16
.
Line 7: We specify safe type casting, preventing any potentially unsafe conversions.
Line 8: We ensure the output array follows a C-style (row-major) memory layout.
Line 9: We set the output array’s data type to int32
.
Line 10: We ensure the output array is a base NumPy array, not a subclass of the input.
Line 13: We display the original array.
Line 14: We display the data type of the original array.
Line 15: We display the result after applying the invert()
function.
Line 16: We display the data type of the result array.
1
(binary00000001
) becomes-2
(binary11111110
).
2
(binary00000010
) becomes-3
(binary11111101
).And so on for the rest of the elements.
When working with larger arrays, numpy.invert()
efficiently performs the bitwise inversion on all elements.
import numpy as nparr = np.array([15, 30, 45, 60], dtype=np.int16)inverted_arr = np.invert(arr,casting='safe',order='C',dtype=np.int32,subok=False)print("Original array:", arr)print("Original dtype:", arr.dtype)print("Inverted array:", inverted_arr)print("Result dtype:", inverted_arr.dtype)
For each element:
15
(binary0000000000011111
forint16
) → inverted to1111111111100000
→ result is-16
inint32
.
30
(binary0000000000011110
) → inverted to1111111111100001
→ result is-31
.And so on for the rest of the elements.
numpy.invert()
numpy.invert()
is most useful in scenarios involving bitwise operations, such as:
Bitwise operations in binary data processing: For tasks such as image processing, encryption, or compression.
Masking or flagging values: In boolean arrays, where you need to invert flags (True/False values).
Working with lower-level data: When dealing with hardware-level or low-level software interfaces that require manipulation of binary representations.
numpy.invert()
operates on integer arrays.
The inversion follows the bitwise NOT rule: flips every bit.
The result of the inversion depends on the size of the integer type and the binary representation of the number.
Negative numbers may appear due to the two’s complement binary representation of negative integers.
The numpy.invert()
function is a handy tool for performing bitwise negation (NOT) operations on arrays in NumPy. Whether you’re dealing with binary data, flags, or low-level bitwise manipulation, this function allows you to efficiently invert the bits of your array elements. It’s especially useful in scientific computing, signal processing, and data analysis tasks where binary operations are commonly used.
For a deeper dive into how numpy.invert()
and other NumPy functions can be applied in real-world scenarios, consider exploring the “Machine Learning with NumPy, pandas, scikit-learn, and More” course, which covers essential techniques for data manipulation and analysis in machine learning.
Haven’t found what you were looking for? Contact Us
Free Resources