【问题标题】:np array as optional argumentsnp 数组作为可选参数
【发布时间】:2015-11-13 01:16:09
【问题描述】:

我有一个函数可以接受两个可选的np.array 作为参数。如果 both 都被传递,函数应该执行一些任务。

def f(some_stuff, this=None, that=None):
    ...do something...
    if this and that:
       perform_the_task()

如果没有传递任何可选参数,这将按预期工作。如果我通过np.array,那么我会得到错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

是否有更紧凑的方法来检查是否通过了额外的参数?我想我可以放心地假设 如果 他们通过了,那么 他们将是np.array

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    首先,您可以假设它们将是正确的类型(特别是如果您是唯一使用该代码的人)。只需将其添加到文档中。 Type checking of arguments Python(接受的答案在他们解释鸭子打字时甚至叹了口气)

    您遇到的异常很常见,而且无处不在,例如ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    在你的情况下,

    if this is not None and that is not None
    

    会完成你想要的。问题不在于“和”,而在于数组

         if np.array([True, False]):
             foo()
    

    从 numpy 的角度来看是模棱两可的,因为 一些 数组的值是真实的,但不是全部......那么它应该返回什么?这种行为与列表的行为明显不一致(我相信您当前的代码遵循 SO 推荐的检查列表是否为 None 或空的方式)

    如果您正在考虑一种 Java esk 方式来根据传递的参数数量来重载您的函数,那么您在 Python 上的思考还不够。您当然可以将条件之后的任何内容发送到另一个函数以“简洁”地处理它。以下也可以

    def f(some_stuff, this=None, that=None):
        ...do something...
    
           perform_the_task(this,that)
    
    def perform_the_task(this,that):
        if this is None or that is None: return
        raise NotImplementedException
    

    【讨论】:

      【解决方案2】:

      正如您在侧边栏中看到的那样,ValueError 之前出现过很多次。

      问题的核心在于 numpy 数组可以返回多个真值,而许多 Python 操作只期望一个。

      我会举例说明:

      In [140]: this=None
      
      In [141]: if this:print 'yes'
      
      In [142]: if this is None: print 'yes'
      yes
      
      In [143]: this=np.array([1,2,3])
      
      In [144]: if this: print 'yes'
      ...
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
      
      In [145]: if this is None: print 'yes'
      
      In [146]: this and this
      ...
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
      
      In [147]: [1,2,3] is None
      Out[147]: False
      In [148]: this is None
      Out[148]: False
      
      In [149]: [1,2,3]==3    # one value
      Out[149]: False     
      In [150]: this == 3    # multiple values
      Out[150]: array([False, False,  True], dtype=bool)
      

      not 这样的逻辑运算通常返回一个简单的真/假,但对于数组,它们为数组的每个元素返回一个值。

      In [151]: not [1,2,3]
      Out[151]: False
      
      In [152]: not None
      Out[152]: True
      
      In [153]: not this
      ...
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
      

      在您的函数中,如果您不提供 f 2 个或更多参数,thisthat 将具有值 None。安全的测试方法是使用is Noneis not None

      def f(some_stuff, this=None, that=None):
          ...do something...
          if this is not None and that is not None:
             perform_the_task()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-10
        • 2016-11-02
        相关资源
        最近更新 更多