📜  np logical_or - Python (1)

📅  最后修改于: 2023-12-03 15:03:16.709000             🧑  作者: Mango

Introduction to np.logical_or in Python

np.logical_or is a NumPy function that performs an element-wise logical OR operation on two input arrays. The output array has the same shape as the input arrays, and each element in the output array is the result of applying the logical OR operation to the corresponding elements in the input arrays.

Syntax

The syntax for using np.logical_or is as follows:

np.logical_or(x1, x2, out=None, where=True, **kwargs)

Here,

  • x1 and x2: The input arrays for which we want to apply the logical OR operation.
  • out (optional): The output array where the result is stored. If not specified, a new array is created.
  • where (optional): A boolean array that specifies which elements of the output array should be replaced with the result. By default, all elements are replaced.
  • kwargs (optional): Additional keyword arguments that are passed to the underlying implementation.
Example Usage
import numpy as np

x1 = np.array([True, False, True])
x2 = np.array([False, False, True])

result = np.logical_or(x1, x2)

print(result)

Output:

[ True False  True]

Here, we create two input arrays x1 and x2 which we pass to np.logical_or, resulting in an output array result. We print result, and we see that the logical OR operation has been applied element-wise to the corresponding elements in the input arrays, resulting in the expected output.

Conclusion

np.logical_or is a useful NumPy function that allows us to perform element-wise logical OR operations on input arrays. We can use it in conjunction with other NumPy functions to perform complex operations on arrays efficiently.