我想加速计算一个公式,用numba执行一个列表理解。

from numba import jit

# General function to generate overlapping windows from a dataframe
@jit
def overlapping_windows(index, wl=256, noverlap=128):
    l = len(index)
    res = [[s,s+wl] for s in xrange(0, l, noverlap) if s+wl < l]
    return res

overlapping_windows([1,2,3,4,5,6,7,8,9,10],4,2)

但是我有一个没有实现的错误。不知道为什么。
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-45-ce0579185abe> in <module>()
      6     return res
      7
----> 8 overlapping_windows([1,2,3,4,5,6,7,8,9,10],4,2)

~/anaconda/lib/python2.7/site-packages/numba/dispatcher.pyc in _compile_and_call(self, *args, **kws)
    123         assert not kws
    124         sig = tuple([typeof_pyval(a) for a in args])
--> 125         self.jit(sig)
    126         return self(*args, **kws)
    127

~/anaconda/lib/python2.7/site-packages/numba/dispatcher.pyc in jit(self, sig, **kws)
    118         """Alias of compile(sig, **kws)
    119         """
--> 120         return self.compile(sig, **kws)
    121
    122     def _compile_and_call(self, *args, **kws):

~/anaconda/lib/python2.7/site-packages/numba/dispatcher.pyc in compile(self, sig, locals, **targetoptions)
    106             cres = compiler.compile_extra(typingctx, targetctx, self.py_func,
    107                                           args=args, return_type=return_type,
--> 108                                           flags=flags, locals=locs)
    109
    110             # Check typing error if object mode is used

~/anaconda/lib/python2.7/site-packages/numba/compiler.pyc in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals)
     85         Use ``None`` to indicate
     86     """
---> 87     bc = bytecode.ByteCode(func=func)
     88     if config.DEBUG:
     89         print(bc.dump())

~/anaconda/lib/python2.7/site-packages/numba/bytecode.pyc in __init__(self, func)
    275             raise ByteCodeSupportError("does not support cellvars")
    276
--> 277         table = utils.SortedMap(ByteCodeIter(code))
    278         labels = set(dis.findlabels(code.co_code))
    279         labels.add(0)

~/anaconda/lib/python2.7/site-packages/numba/utils.pyc in __init__(self, seq)
     44         self._values = []
     45         self._index = {}
---> 46         for i, (k, v) in enumerate(sorted(seq)):
     47             self._index[k] = i
     48             self._values.append((k, v))

~/anaconda/lib/python2.7/site-packages/numba/bytecode.pyc in next(self)
    195             ts = "offset=%d opcode=%x opname=%s"
    196             tv = offset, opcode, dis.opname[opcode]
--> 197             raise NotImplementedError(ts % tv)
    198         if info.argsize:
    199             arg = self.read_arg(info.argsize)

NotImplementedError: offset=66 opcode=5e opname=LIST_APPEND

最佳答案

这很可能意味着您使用的numba版本不支持具有列表理解的函数。

10-07 20:37