Kernel: Python 3
In [2]:
Out[2]:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-1194e744ea9c> in <module>()
1 import time
2 import numpy as np
----> 3 from numba import jit
4 import pandas as pd
ImportError: No module named 'numba'
Example of the problem: integer summation
In [5]:
Out[5]:
9.316529273986816
In [11]:
In [12]:
Out[12]:
4.799173831939697
Precompile the function with @jit
of numba
In [13]:
In [14]:
Out[14]:
0.09396576881408691
In [23]:
A more complicated example with arrays
In [24]:
Out[24]:
array([0, 0, 0, 0])
In [18]:
In [24]:
Out[24]:
0.00023245811462402344
In [25]:
Out[25]:
0.0002434253692626953
In [26]:
Out[26]:
0.0002474784851074219
In [23]:
In [73]:
Out[73]:
10000
In [81]:
In [86]:
Out[86]:
array([[ 0, 1, 2, ..., 9997, 9998, 9999],
[ 0, 1, 2, ..., 9997, 9998, 9999],
[ 0, 1, 2, ..., 9997, 9998, 9999],
...,
[ 0, 1, 2, ..., 9997, 9998, 9999],
[ 0, 1, 2, ..., 9997, 9998, 9999],
[ 0, 1, 2, ..., 9997, 9998, 9999]])
In [20]:
In [21]:
Out[21]:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-21-ad659232eeb3> in <module>()
1 s=time.time()
----> 2 a=f(10000)
3 print(time.time()-s)
/usr/local/lib/python3.5/dist-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
305 argtypes.append(self.typeof_pyval(a))
306 try:
--> 307 return self.compile(tuple(argtypes))
308 except errors.TypingError as e:
309 # Intercept typing error that may be due to an argument
/usr/local/lib/python3.5/dist-packages/numba/dispatcher.py in compile(self, sig)
577
578 self._cache_misses[sig] += 1
--> 579 cres = self._compiler.compile(args, return_type)
580 self.add_overload(cres)
581 self._cache.save_overload(sig, cres)
/usr/local/lib/python3.5/dist-packages/numba/dispatcher.py in compile(self, args, return_type)
78 impl,
79 args=args, return_type=return_type,
---> 80 flags=flags, locals=self.locals)
81 # Check typing error if object mode is used
82 if cres.typing_error is not None and not flags.enable_pyobject:
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
777 pipeline = Pipeline(typingctx, targetctx, library,
778 args, return_type, flags, locals)
--> 779 return pipeline.compile_extra(func)
780
781
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in compile_extra(self, func)
360 self.lifted = ()
361 self.lifted_from = None
--> 362 return self._compile_bytecode()
363
364 def compile_ir(self, func_ir, lifted=(), lifted_from=None):
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in _compile_bytecode(self)
736 """
737 assert self.func_ir is None
--> 738 return self._compile_core()
739
740 def _compile_ir(self):
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in _compile_core(self)
723
724 pm.finalize()
--> 725 res = pm.run(self.status)
726 if res is not None:
727 # Early pipeline completion
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in run(self, status)
246 # No more fallback pipelines?
247 if is_final_pipeline:
--> 248 raise patched_exception
249 # Go to next fallback pipeline
250 else:
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in run(self, status)
238 try:
239 event(stage_name)
--> 240 stage()
241 except _EarlyPipelineCompletion as e:
242 return e.result
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in stage_nopython_backend(self)
656 """
657 lowerfn = self.backend_nopython_mode
--> 658 self._backend(lowerfn, objectmode=False)
659
660 def stage_compile_interp_mode(self):
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in _backend(self, lowerfn, objectmode)
611 self.library.enable_object_caching()
612
--> 613 lowered = lowerfn()
614 signature = typing.signature(self.return_type, *self.args)
615 self.cr = compile_result(typing_context=self.typingctx,
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in backend_nopython_mode(self)
598 self.return_type,
599 self.calltypes,
--> 600 self.flags)
601
602 def _backend(self, lowerfn, objectmode):
/usr/local/lib/python3.5/dist-packages/numba/compiler.py in native_lowering_stage(targetctx, library, interp, typemap, restype, calltypes, flags)
898 lower.lower()
899 if not flags.no_cpython_wrapper:
--> 900 lower.create_cpython_wrapper(flags.release_gil)
901 env = lower.env
902 call_helper = lower.call_helper
/usr/local/lib/python3.5/dist-packages/numba/lowering.py in create_cpython_wrapper(self, release_gil)
227 self.context.create_cpython_wrapper(self.library, self.fndesc,
228 self.env, self.call_helper,
--> 229 release_gil=release_gil)
230
231 def setup_function(self, fndesc):
/usr/local/lib/python3.5/dist-packages/numba/targets/cpu.py in create_cpython_wrapper(self, library, fndesc, env, call_helper, release_gil)
147 fndesc, env, call_helper=call_helper,
148 release_gil=release_gil)
--> 149 builder.build()
150 library.add_ir_module(wrapper_module)
151
/usr/local/lib/python3.5/dist-packages/numba/callwrapper.py in build(self)
120
121 api = self.context.get_python_api(builder)
--> 122 self.build_wrapper(api, builder, closure, args, kws)
123
124 return wrapper, api
/usr/local/lib/python3.5/dist-packages/numba/callwrapper.py in build_wrapper(self, api, builder, closure, args, kws)
174
175 retty = self._simplified_return_type()
--> 176 obj = api.from_native_return(retty, retval, env_manager)
177 builder.ret(obj)
178
/usr/local/lib/python3.5/dist-packages/numba/pythonapi.py in from_native_return(self, typ, val, env_manager)
1323 "prevented the return of " \
1324 "optional value"
-> 1325 out = self.from_native_value(typ, val, env_manager)
1326 return out
1327
/usr/local/lib/python3.5/dist-packages/numba/pythonapi.py in from_native_value(self, typ, val, env_manager)
1337
1338 c = _BoxContext(self.context, self.builder, self, env_manager)
-> 1339 return impl(typ, val, c)
1340
1341 def reflect_native_value(self, typ, val, env_manager=None):
/usr/local/lib/python3.5/dist-packages/numba/targets/boxing.py in box_array(typ, val, c)
307 nativeary = nativearycls(c.context, c.builder, value=val)
308 if c.context.enable_nrt:
--> 309 np_dtype = numpy_support.as_dtype(typ.dtype)
310 dtypeptr = c.env_manager.read_const(c.env_manager.add_const(np_dtype))
311 # Steals NRT ref
/usr/local/lib/python3.5/dist-packages/numba/numpy_support.py in as_dtype(nbtype)
134 return as_dtype(nbtype.dtype)
135 raise NotImplementedError("%r cannot be represented as a Numpy dtype"
--> 136 % (nbtype,))
137
138
NotImplementedError: Failed at nopython (nopython mode backend)
(int64 x 2) cannot be represented as a Numpy dtype
In [19]:
Out[19]:
array([[2, 0]])
In [109]:
In [110]:
Out[110]:
array([1, 2, 1, 2])
In [32]:
Out[32]:
False