簡體   English   中英

NumPy 的 logical_and.reduce 的內部工作原理

[英]Inner workings of NumPy's logical_and.reduce

我想知道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 文檔Method 文檔位於頁面下方的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM