|
|
发表于 2019-9-19 08:59:48
|
显示全部楼层
我最近的尝试是这样的。1、@jit修饰器识别不了pandas的DataFrame数据类型,也就是说当你@jit紧接着定义函数的时候,在函数里的表达式中如果有对DF调用的相关内容都不会进行@jit修饰。2、@jit一般是用于处理大量的数据的,它比较擅长用于对科学实验的大量数据进行处理,你的代码中time()函数第一不是def的。第二也不是大量循环数据处理的。
11111111111111111分割11111111111111111111111
- from numba import jit
- import numpy as np
- x = np.arange(100).reshape(10, 10)
- @jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
- def go_fast(a): # Function is compiled to machine code when called the first time
- trace = 0
- for i in range(a.shape[0]): # Numba likes loops
- trace += np.tanh(a[i, i]) # Numba likes NumPy functions
- return a + trace # Numba likes NumPy broadcasting
- print(go_fast(x))
复制代码
222222222222222222222222分割22222222222222222222222
- from numba import jit
- import pandas as pd
- x = {'a': [1, 2, 3], 'b': [20, 30, 40]}
- @jit
- def use_pandas(a): # Function will not benefit from Numba jit
- df = pd.DataFrame.from_dict(a) # Numba doesn't know about pd.DataFrame
- df += 1 # Numba doesn't understand what this is
- return df.cov() # or this!
- print(use_pandas(x))
复制代码
你可以对比一下包含不包含pandas.@jit作用于def之前,表示告诉你的编译器,可以从@jit的地方开始修饰。
还有一个注意事项,@jit有可选参数的参数nopython=True/False表示在使用@jit修饰器的时候需不需要python自带的修饰器。 |
|