user961627
user961627

Reputation: 12747

explicit signature of python function with parameters including 2d numpy arrays

I want to use @jit or @autojit to speed up my python code, explained here: http://nbviewer.ipython.org/gist/harrism/f5707335f40af9463c43

However the example on that page is for pure python functions, whereas my functions are inside a class, and based on some more searching, it appears that in order for this work with class functions, I must provide the explicit signature of the function.

I haven't worked with signatures before, but I understand now how to use them for functions with simple parameters. But I'm getting stuck on how to write them for complicated parameters such as 2D arrays.

Below is my function for which I need an explicit signature. I really am not sure what to write beyond @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...

Upvotes: 3

Views: 2627

Answers (1)

dustyrockpyle
dustyrockpyle

Reputation: 3184

You use the slice syntax on your data type to represent an array. So your example may look something like:

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

Upvotes: 1

Related Questions