0

我有一个mat类型的矩阵

array([[0.00000000e+00, 1.98300000e+03, 1.57400000e+00, ...,
                   nan,            nan, 2.38395652e+00],
       [0.00000000e+00, 1.98400000e+03, 1.80600000e+00, ...,
                   nan, 1.38395652e+00, 2.29417391e+00],
       [0.00000000e+00, 1.98500000e+03, 4.72400000e+00, ...,
        1.38395652e+00, 1.29417391e+00, 5.68147826e+00],
       ...,
       [9.87500000e+03, 1.99200000e+03, 1.59700000e+00, ...,
                   nan,            nan, 4.61641176e+00],
       [9.87500000e+03, 1.99300000e+03, 3.13400000e+00, ...,
                   nan, 3.61641176e+00, 5.45824421e+00],
       [9.87500000e+03, 1.99400000e+03, 7.61900000e+00, ...,
        3.61641176e+00, 4.45824421e+00, 1.05298571e+01]])

具有尺寸 (107196, 46) 和一个vec类型的向量

array([0.23, 0., 0.28, ..., 0.99, 1.0, 0.05])

尺寸(107196,)。我想使用该np.hstack函数垂直堆叠vec作为mat. 为此,我使用 np.hstack((mat,vec[:,None])) 现在,我想获得一个包含此操作的 njitted 函数。说

@jit(nopython=True)
def simul_nb(matrix, vector):
    return np.hstack((matrix,vector[:,None]))

但是,当我运行时,出现simul_nb(mat,vec)以下错误:

Traceback (most recent call last):

  File "<ipython-input-340-6c5341efa9b6>", line 1, in <module>
    simul_nb(income_df,nols)

  File "C:\Users\bagna\anaconda3\lib\site-packages\numba\core\dispatcher.py", line 415, in _compile_for_args
    error_rewrite(e, 'typing')

  File "C:\Users\bagna\anaconda3\lib\site-packages\numba\core\dispatcher.py", line 358, in error_rewrite
    reraise(type(e), e, None)

  File "C:\Users\bagna\anaconda3\lib\site-packages\numba\core\utils.py", line 80, in reraise
    raise value.with_traceback(tb)

TypingError: No implementation of function Function(<built-in function getitem>) found for signature:
 
getitem(readonly array(float64, 1d, C), Tuple(slice<a:b>, none))
 
There are 16 candidate implementations:
      - Of which 14 did not match due to:
      Overload of function 'getitem': File: <numerous>: Line N/A.
        With argument(s): '(readonly array(float64, 1d, C), Tuple(slice<a:b>, none))':
       No match.
      - Of which 2 did not match due to:
      Overload in function 'GetItemBuffer.generic': File: numba\core\typing\arraydecl.py: Line 162.
        With argument(s): '(readonly array(float64, 1d, C), Tuple(slice<a:b>, none))':
       Rejected as the implementation raised a specific error:
         TypeError: unsupported array index type none in Tuple(slice<a:b>, none)
  raised from C:\Users\bagna\anaconda3\lib\site-packages\numba\core\typing\arraydecl.py:68

During: typing of intrinsic-call at <ipython-input-339-4d5037c266b7> (3)
During: typing of static-get-item at <ipython-input-339-4d5037c266b7> (3)

如何使该功能起作用?

4

1 回答 1

1

Numba 不了解[:,None]用于重塑的索引。实际上,后者等同于[:,np.newaxis],正如您可能已经知道的那样,目前,np.newaxis不是受支持的 numpy features,这部分解释了错误消息。在这里,您应该只使用vector.reshape((-1,1))ornp.expand_dims(vector,1))代替,它应该给出:

@jit(nopython=True)
def simul_nb(matrix, vector):
    return np.hstack((matrix,vector.reshape((-1,1))))

>>> new_mat = simul_nb(matrix, vector)
>>> new_mat.shape
>>> (107196, 47) 
于 2021-02-17T17:35:59.177 回答