不能用numba中的hstack堆叠numpy数组



我有一个类型为

的矩阵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]))现在,我想要得到一个包含这个操作的函数。说

@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:Usersbagnaanaconda3libsite-packagesnumbacoredispatcher.py", line 415, in _compile_for_args
error_rewrite(e, 'typing')
File "C:Usersbagnaanaconda3libsite-packagesnumbacoredispatcher.py", line 358, in error_rewrite
reraise(type(e), e, None)
File "C:Usersbagnaanaconda3libsite-packagesnumbacoreutils.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: numbacoretypingarraydecl.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:Usersbagnaanaconda3libsite-packagesnumbacoretypingarraydecl.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)

如何使函数工作?

Numba不理解用于重塑的[:,None]索引。实际上,正如您可能已经知道的那样,后者相当于[:,np.newaxis],目前,np.newaxis不是受支持的numpy特性,这部分解释了错误消息。在这里,您应该使用vector.reshape((-1,1))np.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) 

相关内容

  • 没有找到相关文章

最新更新