有关numpy认可的特殊方法的文档的位置 [英] Location of documentation on special methods recognized by numpy

查看:39
本文介绍了有关numpy认可的特殊方法的文档的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

math.expnumpy.exp之间的区别之一是,如果您有一个具有C.exp方法的自定义类C,则numpy.exp将注意到并委托给该方法,而math.exp将不是:

One of the differences between math.exp and numpy.exp is that, if you have a custom class C that has a C.exp method, numpy.exp will notice and delegate to this method whereas math.exp will not:

class C:
    def exp(self):
        return 'hey!'

import math
math.exp(C())  # raises TypeError
import numpy
numpy.exp(C())  # evaluates to 'hey!'

但是,如果您访问 numpy.exp ,这似乎是理所当然的.它没有在任何地方明确说明.有记录该功能的地方吗?

However, if you go to the web documentation of numpy.exp, this seems to be taken for granted. It isn't explicitly stated anywhere. Is there a place where this functionality is documented?

更一般地说,是否有一个列表列出了numpy可以识别的所有 all 这样的方法?

More generally, is there a place with a list of all such methods recognized by numpy?

推荐答案

这不是np.exp函数的特殊行为.这只是评估对象dtype数组的结果.

This isn't a special behavior of the np.exp function; it's just a consequence of how object dtype arrays are evaluated.

np.exp会在执行操作之前尝试将非数组输入转换为数组.

np.exp like many numpy functions tries to convert non-array inputs into arrays before acting.

In [227]: class C:
     ...:     def exp(self):
     ...:         return 'hey!'
     ...:     
In [228]: np.exp(C())
Out[228]: 'hey!'

In [229]: np.array(C())
Out[229]: array(<__main__.C object at 0x7feb7154fa58>, dtype=object)
In [230]: np.exp(np.array(C()))
Out[230]: 'hey!'

因此,C()被转换为一个数组,这是一个对象dtype *(C()不是像[1,2,3]这样的可迭代对象).通常,如果给定对象dtype数组,则numpy函数将在元素上进行迭代,要求每个元素执行相应的方法.这就解释了[228]如何最终评估C().exp().

So C() is converted into an array, which is an object dtype *(C() isn't an iterable like [1,2,3]). Typically a numpy function, if given an object dtype array, iterates on the elements, asking each to perform the corresponding method. That explains how [228] ends up evaluating C().exp().

In [231]: np.exp([C(),C()])
Out[231]: array(['hey!', 'hey!'], dtype=object)
In [232]: np.exp([C(),C(),2])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-232-5010b59d525d> in <module>()
----> 1 np.exp([C(),C(),2])

AttributeError: 'int' object has no attribute 'exp'

np.exp可以在数组对象dtype上工作,前提是所有元素都具有exp方法.整数不. ndarray也不是.

np.exp can work on an array object dtype provided all the elements have a exp method. Integers don't. ndarray doesn't either.

In [233]: np.exp([C(),C(),np.array(2)])
AttributeError: 'numpy.ndarray' object has no attribute 'exp'

math.exp需要一个数字,一个Python标量(或可以转换为标量的内容,例如np.array(3).

math.exp expects a number, an Python scalar (or something that can convert into a scalar such as np.array(3).

我希望这种行为是所有ufunc所共有的.我不知道其他不遵循该协议的numpy函数.

I expect this behavior is common to allufunc. I don't know about other numpy functions that don't follow that protocol.

在某些情况下,ufunc委托给__方法:

In some cases the ufunc delegates to __ methods:

In [242]: class C:
     ...:     def exp(self):
     ...:         return 'hey!'
     ...:     def __abs__(self):
     ...:         return 'HEY'
     ...:     def __add__(self, other):
     ...:         return 'heyhey'
     ...:     
In [243]: 
In [243]: np.abs(C())
Out[243]: 'HEY'
In [244]: np.add(C(),C())
Out[244]: 'heyhey'

这篇关于有关numpy认可的特殊方法的文档的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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