OmegaMatze
OmegaMatze

Reputation: 61

Numba with numpy matrix not running -- Unknown attribute

I'm doing the following calculation:

y = np.mat(np.log(datay))   
x = np.mat([datax**2, datax, np.ones(len(datax))]).T
popt = (x.T * x).I * x.T * y.T

datax and datay are normal 1D np.arrays eg:

datay = np.array([1,4,9,16])
datax = np.array([1,2,3,4])

The calcuation is working well. But I wanted to speed it up so I tried to put it in numba: (I am realy new to numba...but want to give it a try)

@jit(nopython=True)
def calc(datax, datay):
    y = np.mat(np.log(datay))   
    x = np.mat([datax**2, datax, np.ones(len(datax))]).T
    return (x.T * x).I * x.T * y.T

but this does not run. I get the following error

Failed at nopython (nopython frontend)

Unknown attribute 'matrix' of type Module numpy

So how can I get that working?


The second thing is: as you might noticed, I'm calculating the parameters of a second order polynomial. I need to get that as fast as possible because i need to do that often. So by now i just loop over all

result = np.zeros(len(datay), 3)
datax  = np.array([1,2,3,4)]  
x = np.matrix([datax**2, datax, np.ones(len(datax))]).T  
for i, data in enumerate(datay):
    data = np.array(data-baseline)
    if (any(i <= 0 for i in data)): continue
    try:
        y = np.matrix(np.log(data))                 
        result[i] = ((x.T * x).I * x.T * y.T).A1

How can I speed that up: Just put all in one numba function and hope that the compiling will do? Or are there other intelligent ways? numba has tools for parallelization right? could they be applied in my case?

Thanks for your time :)

Upvotes: 1

Views: 957

Answers (1)

JoshAdel
JoshAdel

Reputation: 68682

All of the numpy features that numba supports are listed on the following page in the docs:

http://numba.pydata.org/numba-doc/latest/reference/numpysupported.html

Support for numpy matrix objects is not listed, so you can't use them in numba jitted programs currently. If what you are doing is easily vectorized and does not involve creating many intermediate array objects, then the speed-up you get from numba will probably be limited. You could try switching from matrix to array data structures, since the latter is supported. The matrix object just has some slightly different behaviors, so it should be easy to convert the code.

Upvotes: 2

Related Questions