首页 > 解决方案 > 如何在 numba 装饰函数中使用布尔一维数组对一维数组进行子集化?

问题描述

我得说,numba 似乎只能用于经过精心设计以在会谈中呈现的极其简单的用例

我可以很好地运行以下代码:

def rt(hi):
    for i in hi:
        hi_ = i == hi
        t = hi[hi_]
    return None
rt(np.array(['a','b','c','d'],dtype='U'))

但是,当我用以下代码装饰上面的代码时njit

@njit
def rt(hi):
    for i in hi:
        hi_ = i == hi
        t = hi[hi_]
    return None
rt(np.array(['a','b','c','d'],dtype='U'))

我收到以下错误:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-34-eadef1d0ecee> in <module>
      5         t = hi[hi_]
      6     return None
----> 7 rt(np.array(['a','b','c','d'],dtype='U'))

~/miniconda/envs/IndusInd_credit_cards_collections_scorecard_/lib/python3.8/site-packages/numba/core/dispatcher.py in _compile_for_args(self, *args, **kws)
    418                 e.patch_message(msg)
    419 
--> 420             error_rewrite(e, 'typing')
    421         except errors.UnsupportedError as e:
    422             # Something unsupported is present in the user code, add help info

~/miniconda/envs/IndusInd_credit_cards_collections_scorecard_/lib/python3.8/site-packages/numba/core/dispatcher.py in error_rewrite(e, issue_type)
    359                 raise e
    360             else:
--> 361                 raise e.with_traceback(None)
    362 
    363         argtypes = []

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function getitem>) found for signature:
 
 >>> getitem(array([unichr x 1], 1d, C), Literal[bool](False))
 
There are 22 candidate implementations:
      - Of which 20 did not match due to:
      Overload of function 'getitem': File: <numerous>: Line N/A.
        With argument(s): '(array([unichr x 1], 1d, C), bool)':
       No match.
      - Of which 1 did not match due to:
      Overload in function 'GetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 162.
        With argument(s): '(array([unichr x 1], 1d, C), bool)':
       Rejected as the implementation raised a specific error:
         TypeError: unsupported array index type bool in [bool]
  raised from /home/sarthak/miniconda/envs/IndusInd_credit_cards_collections_scorecard_/lib/python3.8/site-packages/numba/core/typing/arraydecl.py:68
      - Of which 1 did not match due to:
      Overload in function 'GetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 162.
        With argument(s): '(array([unichr x 1], 1d, C), Literal[bool](False))':
       Rejected as the implementation raised a specific error:
         TypeError: unsupported array index type Literal[bool](False) in [Literal[bool](False)]
  raised from /home/sarthak/miniconda/envs/IndusInd_credit_cards_collections_scorecard_/lib/python3.8/site-packages/numba/core/typing/arraydecl.py:68

During: typing of intrinsic-call at <ipython-input-34-eadef1d0ecee> (5)

File "<ipython-input-34-eadef1d0ecee>", line 5:
def rt(hi):
    <source elided>
        hi_ = i == hi
        t = hi[hi_]
        ^

如何在 numba 装饰函数中使用布尔一维数组对一维数组进行子集化?

标签: numpynumba

解决方案


推荐阅读