如何覆盖NumPy的ndarray和我的类型之间的比较?

时间:2013-01-31 06:03:27

标签: python numpy operators

在NumPy中,可以使用__array_priority__属性来控制作用于ndarray和用户定义类型的二元运算符。例如:

class Foo(object):
  def __radd__(self, lhs): return 0
  __array_priority__ = 100

a = np.random.random((100,100))
b = Foo()
a + b # calls b.__radd__(a) -> 0
然而,同样的事情似乎并不适合比较运营商。例如,如果我将以下行添加到Foo,则永远不会从表达式a < b调用它:

def __rlt__(self, lhs): return 0

我意识到__rlt__并不是真正的Python特殊名称,但我认为它可能有效。我尝试了__lt____le____eq____ne____ge____gt__的所有内容,包括前导r,加上__cmp__,但我永远不会让NumPy给他们打电话。

这些比较可以被覆盖吗?

更新

为了避免混淆,这里有一个更长的描述NumPy的行为。对于初学者来说,这是NumPy指南中所说的内容:

If the ufunc has 2 inputs and 1 output and the second input is an Object array
then a special-case check is performed so that NotImplemented is returned if the
second input is not an ndarray, has the array priority attribute, and has an
r<op> special method.

我认为这是使+工作的规则。这是一个例子:

import numpy as np
a = np.random.random((2,2))


class Bar0(object):
  def __add__(self, rhs): return 0
  def __radd__(self, rhs): return 1

b = Bar0()
print a + b # Calls __radd__ four times, returns an array
# [[1 1]
#  [1 1]]



class Bar1(object):
  def __add__(self, rhs): return 0
  def __radd__(self, rhs): return 1
  __array_priority__ = 100

b = Bar1()
print a + b # Calls __radd__ once, returns 1
# 1

如您所见,在没有__array_priority__的情况下,NumPy将用户定义的对象解释为标量类型,并在数组中的每个位置应用该操作。那不是我想要的。我的类型是数组(但不应该从ndarray派生)。

这是一个较长的例子,展示了在定义所有比较方法后如何失败:

class Foo(object):
  def __cmp__(self, rhs): return 0
  def __lt__(self, rhs): return 1
  def __le__(self, rhs): return 2
  def __eq__(self, rhs): return 3
  def __ne__(self, rhs): return 4
  def __gt__(self, rhs): return 5
  def __ge__(self, rhs): return 6
  __array_priority__ = 100

b = Foo()
print a < b # Calls __cmp__ four times, returns an array
# [[False False]
#  [False False]]

2 个答案:

答案 0 :(得分:1)

看起来我自己可以回答这个问题。 np.set_numeric_ops可以按如下方式使用:

class Foo(object):
  def __lt__(self, rhs): return 0
  def __le__(self, rhs): return 1
  def __eq__(self, rhs): return 2
  def __ne__(self, rhs): return 3
  def __gt__(self, rhs): return 4
  def __ge__(self, rhs): return 5
  __array_priority__ = 100

def override(name):
  def ufunc(x,y):
    if isinstance(y,Foo): return NotImplemented
    return np.getattr(name)(x,y)
  return ufunc

np.set_numeric_ops(
    ** {
        ufunc : override(ufunc) for ufunc in (
            "less", "less_equal", "equal", "not_equal", "greater_equal"
          , "greater"
          )
    }
  )

a = np.random.random((2,2))
b = Foo()
print a < b
# 4

答案 1 :(得分:0)

我无法重现你的问题。正确的方法是使用__cmp__特殊方法。如果我写

import numpy as np

class Foo(object):
    def __radd__(self, lhs): 
        return 0

    def __cmp__(self, this):
        return -1

    __array_prioriy__ = 100

a = np.random.random((100,100))
b = Foo()
print a<b

并在调试器中设置断点,执行在return -1处停止。

顺便说一下:__array_prioriy__在这里没有任何区别:你有错字!