Python 函数的显式签名,参数包括 2d numpy 数组

问题描述 投票:0回答:1

我想使用

@jit
@autojit
来加速我的 python 代码,解释如下:http://nbviewer.ipython.org/gist/harrism/f5707335f40af9463c43

但是该页面上的示例是针对纯 python 函数的,而我的函数位于类内部,并且基于更多搜索,看来为了与类函数一起工作,我必须提供函数的显式签名。

我以前没有使用过签名,但我现在了解如何将它们用于具有简单参数的函数。但我对如何为复杂的参数(例如二维数组)编写它们感到困惑。

下面是我的函数,我需要显式签名。 除了

@void
我真的不知道该写什么…

""" Function: train
    Input parameters:
    #X =  shape: [n_samples, n_features]
    #y = classes corresponding to X , y's shape: [n_samples]
    #H = int, number of boosting rounds
    Returns: None
    Trains the model based on the training data and true classes
    """
    #@autojit
    #@void
    def train(self, X, y, H):
           # function code below
           # do lots of stuff...
python numba
1个回答
1
投票

您可以在数据类型上使用切片语法来表示数组。所以你的例子可能看起来像:

from numba import void, int_, float_, jit

...

@jit
class YourClass(object):

    ...

    @void(float_[:, :], int_[:], int_)
    def train(self, X, y, H):
         # X is typed as a 2D float array and y as a 1D int array.
         pass
© www.soinside.com 2019 - 2024. All rights reserved.