numba\jit doesn't allow the use of np argsort in nopython mode

£可爱£侵袭症+ 提交于 2019-12-13 07:48:55

问题


Receiving this error message:

Failed at nopython (nopython frontend)

[1m[1m[1mInvalid usage of Function(<function argsort at 0x0000000002A67840>) with parameters (array(float64, 2d, C), axis=int64)

 * parameterized

In definition 0:


While using this code

def rankbids(bids, shifts, groupPeriod, period):
    rowsSize = bids.shape[0];

    finaltable = np.zeros((rowsSize, groupPeriod), dtype=np.float64)
    for i in range(0, period):
        #for 0 to 99
        #CONSTANT 4 UPDATE WHEN NEEDED


        for worker in range(rowsSize):
            shiftNum = int(shifts[worker,i]);

            finaltable[worker, (shiftNum+i*10)] = bids[worker,i];

            if shiftNum == 1:
                finaltable[worker, (shiftNum+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+1+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+2+i*10)] = bids[worker,i];
            elif shiftNum == 2:
                finaltable[worker, (shiftNum+2+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+3+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+4+i*10)] = bids[worker,i];
            elif shiftNum == 3:
                finaltable[worker, (shiftNum+4+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+5+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+6+i*10)] = bids[worker,i];


    indexTable = np.argsort(finaltable, axis=0)
    print(finaltable);




    return finaltable;

rank_bids = numba.jit('float64[:,:](float64[:,:], float64[:,:], int64, float64)', nopython=True)(rankbids);

It seems that numpy does't allow argsort with Nd arrays in numba functions? My question is if anyone has been able to use it within a jit function and perhaps show me what I could possible do to be able to use it!


回答1:


np.argsort works in numba, but not the axis keyword. You could write your code indexTable = np.argsort(finaltable, axis=0) like this:

indexTable = np.empty_like(finaltable)
for j in range(indexTable.shape[1]):
    indexTable[:, j] = np.argsort(finaltable[:, j])


来源:https://stackoverflow.com/questions/51095427/numba-jit-doesnt-allow-the-use-of-np-argsort-in-nopython-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!