无法在numba中创建多维数组

问题描述

我正在尝试执行以下操作:

   {
        "Sid": "","Effect": "Allow","Action": [
            "s3:ListBucket","s3:Getobject"
        ],"Resource": [
            "arn:aws:s3:::account-B-Bucket/*","arn:aws:s3:::account-B-Bucket"
        ]
    }

但这会崩溃

import numpy as np
import numba as nb

@nb.njit
def test(x):
    return np.array([[x,x],[x,x]])

test(np.array([5,5]))

解决方法

In [115]: @nb.njit 
     ...: def test(x): 
     ...:     return np.array([x,x]) 
     ...:     

工作案例:

In [116]: test(10)                                                                                   
Out[116]: array([10,10])

尝试列表-可以但有警告:

In [117]: test([1,2])                                                                                
/usr/local/lib/python3.6/dist-packages/numba/core/ir_utils.py:2031: NumbaPendingDeprecationWarning: 
Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'x' of function 'test'.

For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types

File "<ipython-input-115-c82b85bdc507>",line 2:
@nb.njit
def test(x):
^

  warnings.warn(NumbaPendingDeprecationWarning(msg,loc=loc))
Out[117]: 
array([[1,2],[1,2]])

您的数组输入情况下出现完全错误:

In [118]: test(np.array([1,2]))                                                                      
---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-118-521455fb3f7f> in <module>
----> 1 test(np.array([1,2]))

/usr/local/lib/python3.6/dist-packages/numba/core/dispatcher.py in _compile_for_args(self,*args,**kws)
    413                 e.patch_message(msg)
    414 
--> 415             error_rewrite(e,'typing')
    416         except errors.UnsupportedError as e:
    417             # Something unsupported is present in the user code,add help info

/usr/local/lib/python3.6/dist-packages/numba/core/dispatcher.py in error_rewrite(e,issue_type)
    356                 raise e
    357             else:
--> 358                 reraise(type(e),e,None)
    359 
    360         argtypes = []

/usr/local/lib/python3.6/dist-packages/numba/core/utils.py in reraise(tp,value,tb)
     78         value = tp()
     79     if value.__traceback__ is not tb:
---> 80         raise value.with_traceback(tb)
     81     raise value
     82 

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function array>) found for signature:
 
 >>> array(list(array(int64,1d,C)))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'array': File: numba/core/typing/npydecl.py: Line 504.
    With argument(s): '(list(array(int64,C)))':
   Rejected as the implementation raised a specific error:
     TypingError: array(int64,C) not allowed in a homogeneous sequence
  raised from /usr/local/lib/python3.6/dist-packages/numba/core/typing/npydecl.py:471

During: resolving callee type: Function(<built-in function array>)
During: typing of call at <ipython-input-115-c82b85bdc507> (3)


File "<ipython-input-115-c82b85bdc507>",line 3:
def test(x):
    return np.array([x,x])
    ^

与数组输入配合使用的另一种方法:

In [143]: @nb.njit() 
     ...: def test(x): 
     ...:     temp = np.stack((x,x,x))     # tuple is important
     ...:     return temp.reshape((2,2)+(x.shape)) 

In [147]: test(np.array([1,2]))                                                                      
Out[147]: 
array([[[1,2]],[[1,2]]])