Signup/Sign In

NumPy invert() function

In this tutorial, we will cover the bitwise NOT binary operation of the Numpy library.

  • The invert() function in NumPy is used to compute bit-wise inversion, or bit-wise NOT in an element-wise manner.

  • 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'>

Parameters:

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]

Summary

In this tutorial, we covered the Numpy invert() function. We covered its basic syntax and parameters and then the values returned by this function along with multiple code examples.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.