Using numpy.invert() function (5 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Overview

The numpy.invert() function is a versatile and powerful tool for numerical and scientific computing in Python, particularly when working with bitwise operations on array elements. This tutorial aims to provide a comprehensive guide on how to effectively use the numpy.invert() function across a variety of examples, ranging from basic to advanced applications.

Prerequisites: A working installation of Python and NumPy. Familiarity with NumPy arrays and bitwise operations will be helpful.

Example 1: Basic Usage of numpy.invert()

Let’s start with the basics. The numpy.invert() function performs bitwise NOT operations on array elements. Bitwise NOT operations invert all the bits of an array’s elements. This is how it’s done:

import numpy as np

# Creating a simple array
a = np.array([1, 0, 2, 15], dtype=np.uint8)
# Applying np.invert()
b = np.invert(a)

print("Original array:", a)
print("After np.invert():", b)

Output:

Original array: [1, 0, 2, 15]
After np.invert(): [254, 255, 253, 240]

This example demonstrates the basic application of numpy.invert() on an array of integers. It efficiently inverts the bits of each element.

Example 2: Bitwise NOT on Boolean Arrays

Numpy also allows bitwise operations on boolean arrays. Here’s how you can use numpy.invert() to invert a boolean array:

import numpy as np

# Creating a boolean array
bool_arr = np.array([True, False, True, False])
# Inverting the boolean array using numpy.invert()
inv_bool_arr = np.invert(bool_arr)

print("Original Boolean array:", bool_arr)
print("Inverted Boolean array:", inv_bool_arr)

Output:

Original Boolean array: [True, False, True, False]
Inverted Boolean array: [False, True, False, True]

This demonstrates that numpy.invert() can also be effectively applied to boolean arrays, inverting their truthiness.

Example 3: Using numpy.invert() with Two-dimensional Arrays

The numpy.invert() function can also handle multidimensional arrays. Here’s how to work with a 2D array:

import numpy as np

# Creating a 2D array
array_2d = np.array([[1, 2], [3, 4]], dtype=np.uint8)
# Applying np.invert() to 2D array
inverted_2d = np.invert(array_2d)

print("Original 2D array:\n", array_2d)
print("Inverted 2D array:\n", inverted_2d)

Output:

Original 2D array:
[[1, 2],
 [3, 4]]
Inverted 2D array:
[[254, 253],
 [252, 251]]

This example demonstrates numpy.invert()‘s capability to handle more complex, multidimensional array structures.

Example 4: Boolean Indexing with numpy.invert()

Combining numpy.invert() with boolean indexing can enable powerful data selection techniques. Here’s an advanced usage:

import numpy as np

# Create a sample array
sample_array = np.arange(10)
# Create a boolean array - True for even numbers
bool_index = sample_array % 2 == 0
# Invert the boolean array
cancelling_out = np.invert(bool_index)

# Use inverted boolean indexing to get odds
odds = sample_array[cancelling_out]

print("Original Array:", sample_array)
print("Odd Numbers:", odds)

Output:

Original Array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Odd Numbers: [1, 3, 5, 7, 9]

This example showcases how numpy.invert() can be used in conjunction with boolean indexing to filter arrays based on certain conditions.

Example 5: Applying numpy.invert() on Image Data

As a more complex example, numpy.invert() can be used to invert the colors of an image represented as a numpy array:

from matplotlib import pyplot as plt
import numpy as np

# Load an image (represented as a 3D numpy array)
image = plt.imread('path/to/image.png')

# Invert the image colors
inverted_image = np.invert(image)

# Display the original and inverted images
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title('Original Image')
plt.subplot(1, 2, 2)
plt.imshow(inverted_image)
plt.title('Inverted Image')
plt.show()

This processing technique can have various applications such as data visualization enhancements or artistic effects.

Conclusion

The numpy.invert() function is an essential tool in numpy library, serving numerous purposes from simple bitwise operations to complex data manipulation tasks such as inverting image colors or performing boolean indexing. With the examples discussed, it should be clearer how to leverage this function across different scenarios.