Using numpy.logical_not() function (4 examples)

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

Introduction

The numpy.logical_not() function is a fundamental component of NumPy, a powerful library for numerical computing in Python. This function is used to compute the logical NOT operation element-wise on an input array. In simpler terms, it changes all True values to False, and vice versa. This tutorial will guide you through various examples of using the numpy.logical_not() function, progressing from basic to more advanced scenarios.

Using numpy.logical_not() in Action

Before diving into examples, it’s essential to understand what NumPy is and why numpy.logical_not() is significant. NumPy is a library that provides support for arrays, matrices, and high-level mathematical functions to operate on these data structures. The logical_not() function is a part of NumPy’s logical operations suite, allowing for element-wise inversion of an array’s truth values.

Example 1: Basic Usage of logical_not()

To begin, let’s look at a simple example of using numpy.logical_not() on a boolean array.

import numpy as np

# Creating a boolean array
bool_arr = np.array([True, False, True, False, True])

# Applying logical_not
not_arr = np.logical_not(bool_arr)

print("Original array:", bool_arr)
print("After applying logical_not:", not_arr)

The output will be:

Original array: [True False True False True]
After applying logical_not: [False  True False  True False]

Example 2: Using logical_not() with Numerical Data

It’s also possible to use logical_not() on arrays containing numerical data. In this scenario, non-zero elements are considered True and zero elements as False.

import numpy as np

# Creating a numerical array
num_arr = np.array([1, 0, 1, 0, -1])

# Applying logical_not
not_num_arr = np.logical_not(num_arr)

print("Numerical array:", num_arr)
print("After applying logical_not:", not_num_arr)

The output shows that logical_not() successfully treated non-zero values as True and zero values as False, inverting each accordingly:

Numerical array: [1 0 1 0 -1]
After applying logical_not: [False  True False  True False]

Example 3: Applying logical_not() on a 2D Array

Moving onto more complex data structures, let’s apply logical_not() on a 2-dimensional array.

import numpy as np

# Creating a 2D array
arr_2d = np.array([[True, False], [False, True]])

# Applying logical_not
not_2d = np.logical_not(arr_2d)

print("2D array:", arr_2d)
print("After logical_not:", not_2d)

The output:

2D array: [[True False]
 [False True]]
After logical_not: [[False  True]
 [ True False]]

Example 4: Integrating logical_not() with Other NumPy Functions

For a more advanced scenario, we can combine numpy.logical_not() with other NumPy functions to perform more complex operations. In this example, we will filter an array to obtain only the elements that do not meet a certain condition.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

# Using logical_not with a condition
condition = np.logical_not(arr > 3)

# Filtering the array
filtered_arr = arr[condition]

print("Filtered array:", filtered_arr)

Output:

Filtered array: [1 2 3]

This technique is particularly useful for data manipulation and analysis, offering a concise way to filter arrays based on complex conditions.

Conclusion

In this tutorial, we explored various examples of using the numpy.logical_not() function, from basic boolean arrays to more complex numerical data and integrated use with other NumPy functions. The logical_not function is instrumental in data analysis and manipulation, providing an efficient means to reverse boolean conditions and filter data. Through these examples, you should now have a good understanding of how to utilize numpy.logical_not() in your Python data science projects.