numpy.invert() – The NumPy invert Python Function

Introduction to NumPy invert

The numpy.invert() function is used to compute the element-wise bit-wise inversion of an array. It computes the bitwise NOT of the underlying binary representation of the integers in the input arrays.

The two’s complement is returned for signed integer inputs. Negative numbers are represented as the two’s complement of the absolute value in a two’s complement system.


numpy.invert(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, ufunc ‘invert’)


  • x: [array_like] Input array.
  • out: [ndarray, optional] A location in which the result is stored.
    • If provided, it must have a shape that the inputs broadcast to.
    • If not provided or None, a freshly-allocated array is return.
  • **kwargs: This allows you to pass keyword variable length of argument to a function. It is use when we want to handle a named argument in a function.
  • where: [array_like, optional] True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.

Return: [ndarray or scalar] Result. This is a scalar if x is a scalar.

Code Examples of NumPy invert

Example 01: When the input is a number

# welcome to
# Python program explaining
# invert() function

import numpy as np
num = 5
print ("Input number : ", num)
ans = np.invert(num)
print ("inversion of 5 : ", ans)


Input number :  5
inversion of 5 :  -6

Example 02: When the input is an array

# welcome to
# Python program explaining
# invert() function

import numpy as np

arr = [3, 4, 54]

print ("Input array : ", arr)
ans = np.invert(arr) 
print ("Output array after inversion: ", ans)


Input array :  [3, 4, 54]
Output array after inversion:  [ -4  -5 -55]

Example 03: When inputs are Boolean

# welcome to
# Python program explaining
# invert() function

import numpy as np

bool = [True, False, False, True, False, True]
print("Input array : ", bool)

ans = np.invert(bool)
print ("Output array after inversion: ", ans)


Input array :  [True, False, False, True, False, True]
Output array after inversion:  [False  True  True False  True False]


How do I invert a NumPy image?

In order to invert the picture, we must use NumPy and the broadcasting approach. Each pixel value must be subtract from 255. The reason for this is that in a picture, the highest pixel value or color value is 255. We may use the ‘~’  (negation) operation on the image in either case.

How to inverse a matrix using NumPy

The inverse of a matrix is just a reciprocal of the matrix. As we would do in standard arithmetic for a single integer, which is use to solve equations and determine the value of unknown variables. Inverse of a matrix is the matrix that produces an identity matrix when multiplied by the original matrix. The inverse of a matrix exists only if it is non-singular, that is, if the determinant is not 0. We can easily find the inverse of a square matrix using determinant and adjoint using the formula below.

if det(A) != 0
    A-1 = adj(A)/det(A)
    "Inverse doesn't exist"  


That’s all for this article. If you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. The NumPy bitwise_and Python Function
  2. NumPy bitwise_or Python Function
  3. The NumPy bitwise_xor Python Function

Leave a Comment