NumPy invert() function

  • In case if any signed integer is passed to this function than the 2’s complement of the signed integer will be returned.

Syntax of numpy.invert():

The syntax required to use this function is as follows:

numpy.invert(x, /, out, *, where=True, casting='same_kind', order='K', dtype, subok=True[, signature, extobj]) = <ufunc 'invert'>


Let us now take a look at the parameters of this function:

  • x
    This parameter indicates an input array and with this function, only integer and boolean types are handled.
  • out
    This parameter mainly indicates a location in which the result is stored. If this parameter is provided, it must have a shape that the inputs broadcast to. If this parameter is either not provided or it is None then a freshly-allocated array is returned.
  • where
    This parameter is used to indicate a condition that is broadcast over the input. At those locations where the condition is True, the out array will be set to the ufunc result. Else the out array will retain its original value.

Returned Values:

This function will return a scalar if x is scalar.

Example 1:

In the example below, we will illustrate the usage of the invert() function:

import numpy as np

inp_num = 12
print ("The Input number is: ", inp_num)
outp_num = np.invert(inp_num) 
print ("The inversion of 12 is: ", outp_num)


The Input number is: 12
The inversion of 12 is: -13

Example 2:

In this example, we will use the invert() function with an array of integers:

import numpy as np
inp_arr = [1, 10, 15]
print ("The Input array is: ", inp_arr)
out_arr = np.invert(inp_arr) 
print ("The Output array after inversion: ", out_arr) 


The Input array is: [1, 10, 15]
The Output array after inversion: [ -2 -11 -16]
Follow Us On