(numpy)__array_wrap__做什么? [英] What does (numpy) __array_wrap__ do?

查看:240
本文介绍了(numpy)__array_wrap__做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次进入SciPy LinAlg模块,并且看到了此功能:

I am diving into the SciPy LinAlg module for the first time, and I saw this function:

def _makearray(a):
    new = asarray(a)
    wrap = getattr(a, "__array_prepare__", new.__array_wrap__)
    return new, wrap

__array_wrap__到底能做什么??我发现文档,但我不理解以下解释:

What does __array_wrap__ do exactly? I found the documentation, but I don't understand this explanation:

 At the end of every ufunc, this method is called on the input object with the
 highest array priority, or the output object if one was specified. The ufunc-
 computed array is passed in and whatever is returned is passed to the user. 
 Subclasses inherit a default implementation of this method, which transforms the
 array into a new instance of the object’s class. Subclasses may opt to use this
 method to transform the output array into an instance of the subclass and update
 metadata before returning the array to the user.

这是否仅表示将任何函数的输出转换回array,因为它可能会分解为其他元素以进行逐元素处理?相关地,不管解释如何,将此wrap作为对象意味着什么?你会怎么做?

Does this just mean it reconverts the output of whatever function back into an array since it was likely broken up into something else for element-by-element processing? Relatedly, regardless of the explanation, what would it mean to get this wrap as an object? What would you do with it?

我正在查看numpy.linalg.inv的代码...这里包装的内容是什么?

I am looking at the code for numpy.linalg.inv...what is wrap doing here?

    **a, wrap = _makearray(a)**
    _assertRankAtLeast2(a)
    _assertNdSquareness(a)
    t, result_t = _commonType(a)

    if a.shape[-1] == 0:
        # The inner array is 0x0, the ufunc cannot handle this case
        **return wrap(empty_like(a, dtype=result_t))**

    signature = 'D->D' if isComplexType(t) else 'd->d'
    extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
    ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
    return wrap(ainv.astype(result_t))

推荐答案

np.ma.masked_array.__array_wrap__是更新元数据(mask)的数组子类的示例.

np.ma.masked_array.__array_wrap__ is an example of a array subclass that updates the metadata (the mask).

File:        /usr/lib/python3/dist-packages/numpy/ma/core.py
Definition:  np.ma.masked_array.__array_wrap__(self, obj, context=None)
Source:
    def __array_wrap__(self, obj, context=None):
        """
        Special hook for ufuncs.
        Wraps the numpy array and sets the mask according to context.
        """

np.matrix.__array_wrap__似乎继承了ndarray版本.我的猜测是因为matrix虽然是子类,但没有需要更新的元数据.

np.matrix.__array_wrap__ appears to inherit the ndarray version. My guess it's because matrix, while a subclass, does not have metadata that needs updating.

通常,带有hook的想法是,它是在正常处理过程中被深深调用的函数.默认方法可能不执行任何操作.但这是子类可以采取特殊措施的一种方式.类开发人员这样编写钩子,以便类用户不必担心那些细节.使用__...__名称,它不是公共接口的一部分-尽管Python让我们可以一头雾水.

Generally the idea with a hook, is that it's a function that is called deep within the normal processing. The default method might not do anything. But it's a way that the subclass can take special action. The class developer writes hooks like this so that the class user will not have to worry about those details. With a __...__ name it isn't part of the public interface - though Python lets us peak under the curtain.

wrapping的示例,即返回与输入具有相同类的数组是:

An example of wrapping, i.e returning an array with the same class as the inputs is:

In [659]: np.cumsum(np.arange(10))
Out[659]: array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45], dtype=int32)

In [660]: np.cumsum(np.matrix(np.arange(10)))
Out[660]: matrix([[ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45]], dtype=int32

In [665]: np.cumsum(np.ma.masked_array(np.arange(10)))
Out[665]: 
masked_array(data = [ 0  1  3  6 10 15 21 28 36 45],
             mask = False,
       fill_value = 999999)

返回的值都是相同的,但是数组子类取决于输入类.

The returned values are all the same, but the array subclass varies, depending on the input class.

cumsum可能不是最佳示例.掩码数组具有自己的cumsum版本,该版本将掩码值视为0:

cumsum might not be the best example. Masked arrays have their own version of cumsum, one that treats the masked values as 0:

In [679]: m=np.ma.masked_array(np.arange(10),np.arange(10)%2)

In [680]: m
Out[680]: 
masked_array(data = [0 -- 2 -- 4 -- 6 -- 8 --],
             mask = [False  True False  True False  True False  True False  True],
       fill_value = 999999)

In [681]: np.cumsum(m)
Out[681]: 
masked_array(data = [0 -- 2 -- 6 -- 12 -- 20 --],
             mask = [False  True False  True False  True False  True False  True],
       fill_value = 999999)

add.accumulatecumsum类似,但没有特殊的屏蔽版本:

add.accumulate is similar to cumsum, but doesn't have a special masked version:

In [682]: np.add.accumulate(np.arange(10))
Out[682]: array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45], dtype=int32)

In [683]: np.add.accumulate(m)
Out[683]: 
masked_array(data = [ 0  1  3  6 10 15 21 28 36 45],
             mask = False,
       fill_value = 999999)

最后一个是掩码数组,但是掩码是默认的False,掩码值包括在总和中.

This last is a masked array, but the mask is the default False, and the masked values were included in the sum.

这篇关于(numpy)__array_wrap__做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆