问题
I have a simple function to rank poker hands (the hands are strings).
I call it with rA,rB = rank(a),rank(b)
and here is my implementation. Works well without @jit(nopython=True), but with it, it fails:
File "C:/Users/avi_na/Desktop/poker.py", line 190, in <module>
rA,rB = rank(a),rank(b)
File "C:\Continuum\anaconda3\lib\site-packages\numba\dispatcher.py", line 344, in _compile_for_args
reraise(type(e), e, None)
File "C:\Continuum\anaconda3\lib\site-packages\numba\six.py", line 658, in reraise
raise value.with_traceback(tb)
TypingError: cannot determine Numba type of <class 'builtin_function_or_method'>
from numba import jit
from numba.types import string
@jit(nopython=True)
def rank(hand):
# assert(len(hand) == 5)
rank = "N/A"
p = pd.Series([h[0] for h in hand]).value_counts()
v = sorted(set(pd.Series([h[0] for h in hand]).values), reverse=True)
s = sorted(hand, key=lambda k:k[0])
z = zip(s,s[1:])
if all(x[0]==y[0]-1 for x,y in z):
rank = "Straight "
if len(set([h[1] for h in hand])) == 1:
rank += "Flush "
if "Straight Flush" in rank and sum([h[0] for h in hand]) == sum([10,11,12,13,14]):
rank = "Royal Flush"
elif p[p.idxmax()] == 4:
rank = "4 Of A Kind : %d" % p.idxmax()
elif p[p.idxmax()] == 3 and p[p.idxmin()] == 1:
rank = "3 Of A Kind : %d" % p.idxmax()
elif p[p.idxmax()] == 3 and p[p.idxmin()] == 2:
rank = "Full House : %d,%d" % (p.idxmax(), p.idxmin())
elif p[p.idxmax()] == 2:
max2 = p.nlargest(2)
if list(max2) == [2,2]:
max2 = sorted(list(max2.keys()), reverse=True)
rank = "2 Pairs : %d,%d" % (max2[0],max2[1])
else:
rank = "Pair : %d" % p.idxmax()
else:
rank = "High Card : %d" % v[0]
return rank
回答1:
Pandas and several other function calls in your code will not work with nopython=True
. The available libraries that can be used with numba jit in nopython is fairly limited (pretty much only to numpy arrays and certain python builtin libraries). You can find more information here
回答2:
Per the deprecation recommendations, it's very reasonable that code which doesn't compile with @jit(nopython=True)
could be faster without the decorator.
Anecdotally, I found the trivial example I landed here researching was notably faster when simply passing the Pandas columns directly to my function to vectorize the operation, rather than using numba and paying the method overhead of the columns for a numpy array.
However, it continues with the expected argument to clear this warning
If there is benefit to having the
@jit
decorator present, then to be future proof supply the keyword argumentforceobj=True
to ensure the function is always compiled in object mode.
来源:https://stackoverflow.com/questions/50744686/numba-typingerror-cannot-determine-numba-type-of-class-builtin-function-or