numba

Why is passing a list (of length n) to a numba nopython function an O(n) operation

穿精又带淫゛_ 提交于 2019-12-08 14:26:21
This is only a question to satisfy my curiosity I'm not actually planning on using lists as arguments for a numba function. But I was wondering why passing a list to a numba function seems like an O(n) operation, while it's an O(1) operation in pure-Python functions. Some simple example code: import numba as nb @nb.njit def take_list(lst): return None take_list([1, 2, 3]) # warmup And the timings: for size in [10, 100, 1000, 10000, 100000, 1000000]: lst = [0]*size print(len(lst)) %timeit take_list(lst) # IPythons "magic" timeit Results: 10 4.06 µs ± 26.1 ns per loop (mean ± std. dev. of 7 runs

Parallel programming approach to solve pandas problems

我与影子孤独终老i 提交于 2019-12-08 12:00:41
问题 I have a dataframe of the following format. df A B Target 5 4 3 1 3 4 I am finding the correlation of each column (except Target) with the Target column using pd.DataFrame(df.corr().iloc[:-1,-1]) . But the issue is - size of my actual dataframe is (216, 72391) which atleast takes 30 minutes to process on my system. Is there any way of parallerize it using a gpu ? I need to find the values of similar kind multiple times so can't wait for the normal processing time of 30 minutes each time. 回答1:

Split a dataframe into correspondingly named arrays or series (then recombine)

走远了吗. 提交于 2019-12-08 11:25:59
问题 Let's say I have a dataframe with columns x and y. I'd like to automatically split it into arrays (or series) that have the same names as the columns, process the data, and then later rejoin them. It's pretty straightforward to do this manually: x, y = df.x, df.y z = x + y # in actual use case, there are hundreds of lines like this df = pd.concat([x,y,z],axis=1) But I'd like to automate this. It's easy to get a list of strings with df.columns, but I really want [x,y] rather than ['x','y'].

When numba is effective?

巧了我就是萌 提交于 2019-12-08 07:55:59
问题 I know numba creates some overheads and in some situations (non-intensive computation) it become slower that pure python. But what I don't know is where to draw the line. Is it possible to use order of algorithm complexity to figure out where? for example for adding two arrays (~O(n)) shorter that 5 in this code pure python is faster: def sum_1(a,b): result = 0.0 for i,j in zip(a,b): result += (i+j) return result @numba.jit('float64[:](float64[:],float64[:])') def sum_2(a,b): result = 0.0 for

How to use np.empty inside numba compiled function; Error message “All templates rejected”

让人想犯罪 __ 提交于 2019-12-08 05:52:34
问题 I ran into this weird error when trying to use np.empty in a function definition compiled with numba, and turning on nopython=True to make sure optimized typing is in effect. It's weird because numba claims to support np.empty with the first two arguments, and I am only using the first two arguments (correctly I think?), so I don't know why it's not typing correctly. @jit(nopython=True) def empty(): return np.empty(5, np.float) After defining the above function in an ipython notebook, empty()

Usage of parallel option in numba.jit decoratior makes function give wrong result

风流意气都作罢 提交于 2019-12-07 22:58:34
问题 Given two opposite corners of a rectangle (x1, y1) and (x2, y2) and two radii r1 and r2 , find the ratio of points that lie between the circles defined by the radii r1 and r2 to the total number of points in the rectangle. Simple NumPy approach: def func_1(x1,y1,x2,y2,r1,r2,n): x11,y11 = np.meshgrid(np.linspace(x1,x2,n),np.linspace(y1,y2,n)) z1 = np.sqrt(x11**2+y11**2) a = np.where((z1>(r1)) & (z1<(r2))) fill_factor = len(a[0])/(n*n) return fill_factor Next I tried to optimize this function

Huge errors trying numba

大城市里の小女人 提交于 2019-12-07 16:53:58
问题 I'm running into a big load of errors using numba. Ironically, the correct result is printed after the errors. I'm using the newest Anaconda python and installed numba with conda install numba once on Ubuntu 13, 64 bit and also anaconda 64 bit and on windows 64 bit with a 32 bit version of anaconda. The script I'm trying to execute is: # -*- coding: utf-8 -*- import math from numba import autojit pi = math.pi @autojit def sinc(x): if x == 0.0: return 1.0 else: return math.sin(x*pi)/(pi*x) if

Numba学习日记 —— 2019-12-5

假如想象 提交于 2019-12-07 15:33:26
Numba学习日记 —— 2019-12-5 Python的不足: Python的最大优势也可能是它最大的弱点:它的 灵活性 和 无类型 的高级语法可能导致数据和计算密集型程序的性能不佳。—— 动态类型化解释语言 什么是 numba : Numba,一个来自 Anaconda的Python编译器 ,可以编译Python代码,以便在支持CUDA的GPU或多核CPU上执行。由于Python通常不是编译语言,您可能想知道为什么要使用Python编译器。答案当然是运行本机编译代码比运行动态解释代码快许多倍。 Numba允许您为Python函数指定类型签名,它可以在运行时进行编译(这是 “即时”或JIT编译 )。 Numba动态编译代码的能力意味着您不会放弃Python的灵活性。这是向高效率编程和高性能计算提供理想组合的重要一步。 使用Numba,现在可以 编写标准的Python函数并在支持CUDA的GPU上运行它们 。 Numba专为面向阵列的计算任务而设计,就像广泛使用的NumPy库一样。面向阵列的计算任务中的数据并行性非常适合GPU等加速器。 Numba了解NumPy数组类型,并使用它们生成有效的编译代码,以便在GPU或多核CPU上执行。 所需的编程工作可以像添加函数装饰器一样简单 ,以指示Numba为GPU编译。例如,以下代码中的

Using Dictionaries with numba njit function

痞子三分冷 提交于 2019-12-07 14:45:22
问题 How to speed up a funtion with numba when input and return are dictionaries? I'm familiar with using numba for functions that accept numbers and return arrays, like this: @numba.jit('float64[:](int32,int32)',nopython=True) def f(a, b): # returns array 1d array Now I have a function that accepts and returns dictionaries. How can I apply numba here? def collocation(aeolus_data,val_data): ... return sample_aeolus, sample_valdata 回答1: The support for Dictionary has now been added in Numba version

How to use np.empty inside numba compiled function; Error message “All templates rejected”

对着背影说爱祢 提交于 2019-12-07 13:36:28
I ran into this weird error when trying to use np.empty in a function definition compiled with numba, and turning on nopython=True to make sure optimized typing is in effect. It's weird because numba claims to support np.empty with the first two arguments, and I am only using the first two arguments (correctly I think?), so I don't know why it's not typing correctly. @jit(nopython=True) def empty(): return np.empty(5, np.float) After defining the above function in an ipython notebook, empty() Gives the following error message: -------------------------------------------------------------------