包含2D numpy数组参数的Python函数的显式签名
我想用 @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...
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