Compute the truth value of NOT an array element-wise in Numpy

NumpyServer Side ProgrammingProgramming

To compute the truth value of NOT an array element-wise, use the numpy.logical_not() method in Python Numpy. Return value is either True or False.

Return value is the boolean result with the same shape as x of the NOT operation on elements of x. This is a scalar if x is a scalar. The out is a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

Creating a 2D numpy array using the array() method. We have inserted elements −

arr = np.array([[True, False, True], [True, True, False]])

Display the array −

print("Array...\n", arr)

Get the type of the array −

print("\nOur Array type...\n", arr.dtype)

Get the dimension of the array −

print("\nOur Array Dimension...\n",arr.ndim)

Get the shape of the array −

print("\nOur Array Shape...\n",arr.shape)

To compute the truth value of NOT an array element-wise, use the numpy.logical_not() method in Python Numpy. Return value is either True or False −

print("\nResult (NOT)...\n",np.logical_not(arr))

Example

import numpy as np

# Creating a 2D numpy array using the array() method
# We have inserted elements
arr = np.array([[True, False, True], [True, True, False]])

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimension of the array
print("\nOur Array Dimension...\n",arr.ndim)

# Get the shape of the array
print("\nOur Array Shape...\n",arr.shape)

# To compute the truth value of NOT an array element-wise, use the numpy.logical_not() method in Python Numpy
# Return value is either True or False
print("\nResult (NOT)...\n",np.logical_not(arr))

Output

Array...
[[ True False True]
[ True True False]]

Our Array type...
bool

Our Array Dimension...
2

Our Array Shape...
(2, 3)

Result (NOT)...
[[False True False]
[False False True]]
raja
Updated on 05-Feb-2022 12:13:38

Advertisements