NumPy Invert Function

PythonPythonBeginner
Practice Now

Introduction

NumPy is a popular Python library used for scientific computing, especially for numerical computations and analysis. One of the many functions provided by NumPy is the "Invert" function. In this lab, we will take a step-by-step look at how to use the NumPy Invert function to compute a bitwise inversion.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/booleans -.-> lab-86454{{"`NumPy Invert Function`"}} python/lists -.-> lab-86454{{"`NumPy Invert Function`"}} python/tuples -.-> lab-86454{{"`NumPy Invert Function`"}} python/importing_modules -.-> lab-86454{{"`NumPy Invert Function`"}} python/numerical_computing -.-> lab-86454{{"`NumPy Invert Function`"}} numpy/bool_idx -.-> lab-86454{{"`NumPy Invert Function`"}} numpy/fancy_idx -.-> lab-86454{{"`NumPy Invert Function`"}} end

Import NumPy library

To use the NumPy library in your Python code, you must first import it. Here's an example of how to do that:

import numpy as np

Understand the NumPy Invert function

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 the NumPy Invert function

The following syntax is required to use the NumPy Invert function:

 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.

Using the NumPy Invert function with a scalar value

The example below illustrates the usage of the Invert function with a scalar value:

    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)

Output:

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

Using the NumPy Invert function with an array of integers

This example illustrates the usage of 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)

Output:

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

Summary

In this lab, we covered the NumPy Invert function. We covered its basic syntax and parameters as well as the values returned by this function along with multiple code examples.

Other Python Tutorials you may like