Skip to main content
 首页 » 编程设计

python之NumPy 的 logical_and.reduce 的内部工作原理

2024年11月24日15dyllove98

我想知道 np.logical_and.reduce() 是如何工作的。

如果我看logical_and文档将其呈现为具有某些参数的函数。但是当它与 reduce 一起使用时,它不会获得任何参数。

当我查看 reduce 时文档我可以看到它有 ufunc.reduce 作为它的定义。所以我想知道,当我调用 np.logical_and.reduce() 时使用了什么样的机制? logical_and 作为 ufunc 在该片段中代表什么:函数、对象或其他东西?

请您参考如下方法:

我不确定你的问题是什么。使用 Pythons 帮助减少的参数如下所示。 reduce 充当 ufunc 的一个方法,它是在运行时接受参数的 reduce。

In [1]: import numpy as np 
 
help(np.logical_and.reduce) 
Help on built-in function reduce: 
reduce(...) method of numpy.ufunc instance 
    reduce(a, axis=0, dtype=None, out=None, keepdims=False) 
    Reduces `a`'s dimension by one, by applying ufunc along one axis. 

玩这个:

a=np.arange(12.0)-6.0 
a.shape=3,4 
a 
Out[6]: 
array([[-6., -5., -4., -3.], 
       [-2., -1.,  0.,  1.], 
       [ 2.,  3.,  4.,  5.]]) 
 
np.logical_and.reduce(a, axis=0) 
Out[7]: array([ True,  True, False,  True], dtype=bool) 
# False for zero in column 2 
 
np.logical_and.reduce(a, axis=1) 
Out[8]: array([ True, False,  True], dtype=bool) 
# False for zero in row 1 

如果保留尺寸,可能会更清楚。

np.logical_and.reduce(a, axis=0, keepdims=True) 
Out[12]: array([[ True,  True, False,  True]], dtype=bool) 
 
np.logical_and.reduce(a, axis=1, keepdims=True) 
Out[11]: 
array([[ True], 
       [False],    # Row 1 contains a zero. 
       [ True]], dtype=bool) 

沿所选轴的每个元素的缩减和 远期买入的累积结果。这是 Python 的等价物,我相信 numpy 会更有效率。

res=a[0]!=0     # The initial value for result bought forward 
for arr in (a!=0)[1:]: 
    print(res, arr) 
    res = np.logical_and(res, arr)  # logical and res and a!=0 
print('\nResult: ', res) 
 
Out: 
[ True  True  True  True] [ True  True False  True] 
[ True  True False  True] [ True  True  True  True] 
 
Result:  [ True  True False  True] 

希望这有助于或有助于澄清您的问题。

编辑:链接到文档和可调用对象示例。

ufunc documentation方法文档位于页面下方约 60%。

要理解可调用的方法,这里有一个 ListUfunc 类,它给出了用于 Python 列表的 numpy ufunc 的非常基本的示例。

class ListUfunc: 
    """ Create 'ufuncs' to process lists. """ 
    def __init__(self, func, init_reduce=0): 
        self._do = func   # _do is the scalar func to apply. 
        self.reduce0 = init_reduce  # The initial value for the reduction method 
        # Some reductions start from zero, logical and starts from True 
    def __call__(self, a, b): 
        """ Apply the _do method to each pair of a and b elements. """ 
        res=[] 
        for a_item, b_item in zip(a, b): 
            res.append(self._do(a_item, b_item)) 
        return res 
 
    def reduce(self, lst): 
        bfwd = self.reduce0 
        for item in lst: 
            bfwd = self._do(bfwd, item) 
        return bfwd 
 
a=range(12) 
b=range(12,24)     
 
plus = ListUfunc(lambda a, b : a+b) 
plus(a, b) 
Out[6]: [12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34] 
plus.reduce(a) 
Out[7]: 66 
plus.reduce(b) 
Out[8]: 210 
 
log_and = ListUfunc( lambda a, b: bool(a and b), True ) 
log_and(a,b) 
Out[25]: [False, True, True, True, True, True, True, True, True, True, True, True] 
log_and.reduce(a) 
Out[27]: False  # a contains a zero 
 
log_and.reduce(b) 
Out[28]: True  # b doesn't contain a zero