What is numpy.invert() in NumPy?

The invert() function is one of the many useful functions provided by NumPy.

The invert() function is used to invert the NumPy array. For example, if the array is a signed integer, then the invert() function will return its 2’s complement. In medical image processing, if the color of the image is black, then the invert() function will convert its color to white, and vice versa.

Syntax

This is the syntax that is required to use this function:

numpy.invert(array)

Parameter

  • array: This parameter denotes the input array. Please note that this function only supports integer and Boolean types.

Return value

If the array parameter value is a scalar, this function will return scalar.

Code example 1

We’ll use the invert() method with an array of signed numbers in this example:

import numpy as np
arr = [-8, 20, -65]
print ("The Input array is: ", arr)
result = np.invert(arr)
print ("The Output array after inversion: ", result)

Code example 2

We’ll use the invert() method with an array of unsigned numbers in this example:

import numpy as np
arr = [4, 56, 89]
print ("The Input array is: ", arr)
result = np.invert(arr)
print ("The Output array after inversion: ", result)

Conclusion

In this shot, we went through the invert() function’s syntax, parameters, the values returned by it, and two code examples of the function in use.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved