Compute the truth value of an array OR to another array element-wise in Numpy

NumpyServer Side ProgrammingProgramming

To compute the truth value of an array OR another array element-wise, use the numpy.logical_or() method in Python Numpy. Return value is either True or False. Return value is the boolean result of the logical OR operation applied to the elements of x1 and x2; the shape is determined by broadcasting. This is a scalar if both x1 and x2 are scalars

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.

Steps

At first, import the required library −

import numpy as np

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

arr1 = np.array([[True, False, False], [False, True, True]])
arr2 = np.array([[True, False, True], [True, True, False]])

Display the arrays −

print("Array 1...\n", arr1)
print("\nArray 2...\n", arr2)

Get the type of the arrays −

print("\nOur Array 1 type...\n", arr1.dtype)
print("\nOur Array 2 type...\n", arr2.dtype)

Get the dimensions of the Arrays −

print("\nOur Array 1 Dimensions...\n",arr1.ndim)
print("\nOur Array 2 Dimensions...\n",arr2.ndim)

Get the shape of the Arrays −

print("\nOur Array 1 Shape...\n",arr1.shape)
print("\nOur Array 2 Shape...\n",arr2.shape)

To compute the truth value of an array OR another array element-wise, use the numpy.logical_or() method. Return value is either True or False −

print("\nResult (OR)...\n",np.logical_or(arr1, arr2))

Example

import numpy as np

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

# Display the arrays
print("Array 1...\n", arr1)
print("\nArray 2...\n", arr2)

# Get the type of the arrays
print("\nOur Array 1 type...\n", arr1.dtype)
print("\nOur Array 2 type...\n", arr2.dtype)

# Get the dimensions of the Arrays
print("\nOur Array 1 Dimensions...\n",arr1.ndim)
print("\nOur Array 2 Dimensions...\n",arr2.ndim)

# Get the shape of the Arrays
print("\nOur Array 1 Shape...\n",arr1.shape)
print("\nOur Array 2 Shape...\n",arr2.shape)

# To compute the truth value of an array OR another array elementwise, use the numpy.logical_or() method in Python Numpy
# Return value is either True or False
print("\nResult (OR)...\n",np.logical_or(arr1, arr2))

Output

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

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

Our Array 1 type...
bool

Our Array 2 type...
bool

Our Array 1 Dimensions...
2

Our Array 2 Dimensions...
2

Our Array 1 Shape...
(2, 3)

Our Array 2 Shape...
(2, 3)

Result (OR)...
[[ True False True]
[ True True True]]
raja
Updated on 08-Feb-2022 11:03:13

Advertisements