def logical_not(x, *args, **kwargs):
Parameters
----------
x : array_like
Returns
-------
y : bool or ndarray of bool
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values
of False indicate to leave the value in the output alone.
Examples
--------
>>> np.logical_not(3)
False
>>> np.logical_not([True, False, 0, 1])
array([False, True, True, False])
>>> x = np.arange(5)
>>> np.logical_not(x<3)
array([False, False, False, True, True])
"""
import numpy as np
threshold=3
X= np.array([1,2,3,1,2,4,5,0,3,6,2])
print(X> threshold)
#[False False False False False True True False False True False]
cond = X > threshold
not_cond = np.logical_not(cond)
X[cond] = 1
X[not_cond] = 0
print(X)
def logical_and(x1, x2, *args, **kwargs):
Parameters
----------
x1, x2 : array_like
Input arrays. `x1` and `x2` must be of the same shape.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values
of False indicate to leave the value in the output alone.
Returns
-------
y : ndarray or bool
Boolean result with the same shape as `x1` and `x2` of the logical
AND operation on corresponding elements of `x1` and `x2`.
This is a scalar if both `x1` and `x2` are scalars.
Examples
--------
>>> np.logical_and(True, False)
False
>>> np.logical_and([True, False], [False, False])
array([False, False])
>>> x = np.arange(5)
>>> np.logical_and(x>1, x<4)
array([False, False, True, True, False])
"""
def logical_or(x1, x2, *args, **kwargs):
Parameters
----------
x1, x2 : array_like
Logical OR is applied to the elements of `x1` and `x2`.
They have to be of the same shape.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values
of False indicate to leave the value in the output alone.
Returns
-------
y : ndarray or bool
Boolean result with the same shape as `x1` and `x2` of the logical
OR operation on elements of `x1` and `x2`.
This is a scalar if both `x1` and `x2` are scalars.
Examples
--------
>>> np.logical_or(True, False)
True
>>> np.logical_or([True, False], [False, False])
array([ True, False])
>>> x = np.arange(5)
>>> np.logical_or(x < 1, x > 3)
array([ True, False, False, False, True])
"""
def logical_xor(x1, x2, *args, **kwargs):
x1, x2 : array_like
Logical XOR is applied to the elements of `x1` and `x2`. They must
be broadcastable to the same shape.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values
of False indicate to leave the value in the output alone.
Returns
-------
y : bool or ndarray of bool
Boolean result of the logical XOR operation applied to the elements
of `x1` and `x2`; the shape is determined by whether or not
broadcasting of one or both arrays was required.
This is a scalar if both `x1` and `x2` are scalars.
Examples
--------
>>> np.logical_xor(True, False)
True
>>> np.logical_xor([True, True, False, False], [True, False, True, False])
array([False, True, True, False])
>>> x = np.arange(5)
>>> np.logical_xor(x < 1, x > 3)
array([ True, False, False, False, True])
Simple example showing support of broadcasting
>>> np.logical_xor(0, np.eye(2))
array([[ True, False],
[False, True]])
"""
NumPy “bitwise_” 开头的函数是位运算函数。
bitwise_and
bitwise_and() 函数
对数组中整数的二进制形式执行位与运算。
import numpy as np
print ('13 和 17 的二进制形式:')
a,b = 13,17
print (bin(a), bin(b))
print ('\n')
13 和 17 的二进制形式:
0b1101 0b10001
print ('13 和 17 的位与:')
print (np.bitwise_and(13, 17))
13 和 17 的位与:
1
bitwise_or
bitwise_or()函数对数组中整数的二进制形式执行位或运算。
import numpy as np
a,b = 13,17
print ('13 和 17 的二进制形式:')
print (bin(a), bin(b))
print ('13 和 17 的位或:')
print (np.bitwise_or(13, 17))
13 和 17 的二进制形式:
0b1101 0b10001
13 和 17 的位或:
29
invert
invert() 函数
对数组中整数进行位取反运算,即 0 变成 1,1 变成 0。
对于有符号整数,取该二进制数的补码,然后 +1。二进制数,最高位为0表示正数,最高位为 1 表示负数。
import numpy as np
print ('13 的位反转,其中 ndarray 的 dtype 是 uint8:')
print (np.invert(np.array([13], dtype = np.uint8)))
print ('\n')
# 比较 13 和 242 的二进制表示,我们发现了位的反转
print ('13 的二进制表示:')
print (np.binary_repr(13, width = 8))
print ('\n')
print ('242 的二进制表示:')
print (np.binary_repr(242, width = 8))
13 的位反转,其中 ndarray 的 dtype 是 uint8:
[242]
13 的二进制表示:
00001101
242 的二进制表示:
11110010
left_shift
left_shift() 函数将数组元素的二进制形式向左移动到指定位置,右侧附加相等数量的 0。
import numpy as np
print ('将 10 左移两位:')
print (np.left_shift(10,2))
print ('\n')
print ('10 的二进制表示:')
print (np.binary_repr(10, width = 8))
print ('\n')
print ('40 的二进制表示:')
print (np.binary_repr(40, width = 8))
# '00001010' 中的两位移动到了左边,并在右边添加了两个 0。
将 10 左移两位:
40
10 的二进制表示:
00001010
40 的二进制表示:
00101000
right_shift
right_shift() 函数将数组元素的二进制形式向右移动到指定位置,左侧附加相等数量的 0。
import numpy as np
print ('将 40 右移两位:')
print (np.right_shift(40,2))
print ('\n')
print ('40 的二进制表示:')
print (np.binary_repr(40, width = 8))
print ('\n')
print ('10 的二进制表示:')
print (np.binary_repr(10, width = 8))
# '00001010' 中的两位移动到了右边,并在左边添加了两个 0。
将 40 右移两位:
10
40 的二进制表示:
00101000
10 的二进制表示:
00001010