python – (numpy)__array_wrap__做什么?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – (numpy)__array_wrap__做什么?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3357字,纯文字阅读大概需要5分钟。
内容图文
![python – (numpy)__array_wrap__做什么?](/upload/InfoBanner/zyjiaocheng/795/068bc47b74d248cba53dc2d8b5873ced.jpg)
我第一次潜入SciPy LinAlg模块,我看到了这个功能:
def _makearray(a):
new = asarray(a)
wrap = getattr(a, "__array_prepare__", new.__array_wrap__)
return new, wrap
__array_wrap__到底做了什么?我找到了documentation,但我不明白这个解释:
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.
这是否意味着它将任何函数的输出重新转换回数组,因为它可能被分解为其他元素以进行逐元素处理?相关地,无论解释如何,将此包装作为对象意味着什么?你会怎么做?
我正在查看numpy.linalg.inv的代码…这里的包装是什么?
**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__是更新元数据(掩码)的数组子类的示例.
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版本.我的猜测是因为矩阵虽然是子类,但没有需要更新的元数据.
通常使用钩子的想法是,它是在正常处理中被深度调用的函数.默认方法可能不会执行任何操作.但这是子类可以采取特殊操作的方式.类开发人员编写这样的钩子,以便类用户不必担心这些细节.使用__…__名称它不是公共接口的一部分 – 尽管Python让我们在幕后巅峰.
包装的一个例子,即返回与输入具有相同类的数组是:
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)
返回的值都是相同的,但数组子类会有所不同,具体取决于输入类.
cumsum可能不是最好的例子.蒙面数组有自己的cumsum版本,一个将屏蔽值视为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.accumulate与cumsum类似,但没有特殊的蒙版版本:
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,掩码值包含在总和中.
内容总结
以上是互联网集市为您收集整理的python – (numpy)__array_wrap__做什么?全部内容,希望文章能够帮你解决python – (numpy)__array_wrap__做什么?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。