numba - TypingError: cannot determine Numba type of <class 'builtin_function_or_method'>
numba - TypingError: cannot determine Numba type of <class 'builtin_function_or_method'>
我有一个简单的函数来对扑克手牌进行排名(手牌是字符串)。
我用 rA,rB = rank(a),rank(b)
调用它,这是我的实现。没有 @jit(nopython=True) 也能很好地工作,但是有了它,它就失败了:
File "...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
Pandas 和代码中的其他几个函数调用将不适用于 nopython=True
。在 nopython 中可与 numba jit 一起使用的可用库相当有限(几乎仅适用于 numpy 数组和某些 python 内置库)。您可以找到更多信息here
根据 deprecation recommendations,不使用 @jit(nopython=True)
编译的代码在没有装饰器的情况下更快是非常合理的.
有趣的是,我发现我在这里研究的这个小例子在简单地将 Pandas 列直接传递到我的函数到 时明显更快,而不是使用 numba 并支付方法开销numpy 数组的列。
但是,它会继续使用预期的参数来清除此警告
If there is benefit to having the @jit
decorator present, then to be future proof supply the keyword argument forceobj=True
to ensure the function is always compiled in object mode.
我有一个简单的函数来对扑克手牌进行排名(手牌是字符串)。
我用 rA,rB = rank(a),rank(b)
调用它,这是我的实现。没有 @jit(nopython=True) 也能很好地工作,但是有了它,它就失败了:
File "...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
Pandas 和代码中的其他几个函数调用将不适用于 nopython=True
。在 nopython 中可与 numba jit 一起使用的可用库相当有限(几乎仅适用于 numpy 数组和某些 python 内置库)。您可以找到更多信息here
根据 deprecation recommendations,不使用 @jit(nopython=True)
编译的代码在没有装饰器的情况下更快是非常合理的.
有趣的是,我发现我在这里研究的这个小例子在简单地将 Pandas 列直接传递到我的函数到
但是,它会继续使用预期的参数来清除此警告
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.