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



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...
", arr)

Get the type of the array −

print("
Our Array type...
", arr.dtype)

Get the dimension of the array −

print("
Our Array Dimension...
",arr.ndim)

Get the shape of the array −

print("
Our Array Shape...
",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("
Result (NOT)...
",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...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimension of the array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the array print("
Our Array Shape...
",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("
Result (NOT)...
",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]]
Updated on: 2022-02-05T12:13:38+05:30

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements