In NumPy, logical operation methods are used to perform logical operations on elements in an array. They are usually used in boolean arrays, and can also be used in numeric arrays. Non-zero values are considered asTrue
, zero value is consideredFalse
. Common logical operations are:
1. numpy.logical_and
Perform logic and operation (AND) on an element-by-element basis only if the elements in the corresponding positions of the two arrays areTrue
When , the result isTrue
。
Example:
import numpy as np a = ([True, False, True, False]) b = ([True, True, False, False]) result = np.logical_and(a, b) print(result) # [ True False False False]
2. numpy.logical_or
Perform logical or operation (OR) elements, as long as there is an element in the corresponding position in the two arrays.True
, the result isTrue
。
Example:
import numpy as np a = ([True, False, True, False]) b = ([True, True, False, False]) result = np.logical_or(a, b) print(result) # [ True True True False]
3. numpy.logical_xor
Perform logical XOR operation (XOR) on an element-by-element basis. When the elements in the corresponding positions in the two arrays are different, the result isTrue
。
Example:
import numpy as np a = ([True, False, True, False]) b = ([True, True, False, False]) result = np.logical_xor(a, b) print(result) # [False True True False]
4. numpy.logical_not
Perform logical non-operation (NOT) on an element-by-element basis, andTrue
Convert toFalse
,WillFalse
Convert toTrue
。
Example:
import numpy as np a = ([True, False, True, False]) result = np.logical_not(a) print(result) # [False True False True]
5.
Compare whether two arrays are equal element by element. If equal, returnTrue
; Otherwise returnFalse
。
Example:
import numpy as np a = ([1, 2, 3]) b = ([1, 2, 4]) result = (a, b) print(result) # [ True True False]
6. numpy.not_equal
Compare elements by element to whether two arrays are not equal. If not equal, returnTrue
; Otherwise returnFalse
。
Example:
import numpy as np a = ([1, 2, 3]) b = ([1, 2, 4]) result = np.not_equal(a, b) print(result) # [False False True]
7.
Compare two arrays by element. If the element of the first array is larger than the element of the second array, returnTrue
。
Example:
import numpy as np a = ([1, 2, 3]) b = ([1, 2, 2]) result = (a, b) print(result) # [False False True]
8. numpy.greater_equal
Compare two arrays by element. If the element of the first array is greater than or equal to the element of the second array, returnTrue
。
Example:
import numpy as np a = ([1, 2, 3]) b = ([1, 2, 2]) result = np.greater_equal(a, b) print(result) # [ True True True]
9.
Compare two arrays by element. If the element of the first array is smaller than the element of the second array, returnTrue
。
Example:
import numpy as np a = ([1, 2, 3]) b = ([1, 2, 4]) result = (a, b) print(result) # [False False True]
10. numpy.less_equal
Compare two arrays by element. If the element of the first array is less than or equal to the element of the second array, returnTrue
。
Example:
import numpy as np a = ([1, 2, 3]) b = ([1, 2, 4]) result = np.less_equal(a, b) print(result) # [ True True True]
11. numpy.bitwise_and
Perform bit and operations by element (usually used in integer arrays). andlogical_and
Similar, butbitwise_and
Processing binary representations of integers.
Example:
import numpy as np a = ([1, 0, 1, 0], dtype=int) b = ([1, 1, 0, 0], dtype=int) result = np.bitwise_and(a, b) print(result)
12. numpy.bitwise_or
Perform bits or operations by element for binary representation of integers.
Example:
import numpy as np a = ([1, 0, 1, 0], dtype=int) b = ([1, 1, 0, 0], dtype=int) result = np.bitwise_or(a, b) print(result)
13. numpy.bitwise_xor
Perform bit exclusive OR operation by element, used for binary representation of integers.
Example:
import numpy as np a = ([1, 0, 1, 0], dtype=int) b = ([1, 1, 0, 0], dtype=int) result = np.bitwise_xor(a, b) print(result)
Summarize
These logical operation methods can easily compare elements in an array and logical operations on elements by element. They are widely used in array filtering, selection, conditional judgment and masking operations.
This is the end of this article about Python numpy logical operation methods. For more related numpy logical operation methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!