numba

How to parallelize this Python for loop when using Numba

北城以北 提交于 2019-11-29 01:32:08
I'm using the Anaconda distribution of Python, together with Numba, and I've written the following Python function that multiplies a sparse matrix A (stored in a CSR format) by a dense vector x : @jit def csrMult( x, Adata, Aindices, Aindptr, Ashape ): numRowsA = Ashape[0] Ax = numpy.zeros( numRowsA ) for i in range( numRowsA ): Ax_i = 0.0 for dataIdx in range( Aindptr[i], Aindptr[i+1] ): j = Aindices[dataIdx] Ax_i += Adata[dataIdx] * x[j] Ax[i] = Ax_i return Ax Here A is a large scipy sparse matrix, >>> A.shape ( 56469, 39279 ) # having ~ 142,258,302 nonzero entries (so about 6.4% ) >>> type(

Getting python Numba working on Ubuntu 14.10 or Fedora 21 with python 2.7

余生颓废 提交于 2019-11-28 22:49:25
Recently, I have had a frustrating time to get python Numba working on Ubuntu or Fedora Linux. The main problem has been with the compilation of llvmlite. What do I need to install for these to compile properly? Alexis King The versions I got working at the end were numba-0.17.0 (also 0.18.2) and llvmlite-0.2.2 (also 0.4.0). Here are the relevant dependencies and configuration options on Ubuntu and Fedora. For Ubuntu 14.04 *Trusty) sudo apt-get install zlib1g zlib1g-dev libedit libedit-dev llvm-3.8 llvm-3.8-dev llvm-dev sudo pip install enum34 funcsigs LLVM_CONFIG=/usr/bin/llvm-config-3.8 pip

running librosa & numba on raspberry pi 3

夙愿已清 提交于 2019-11-28 12:50:29
I am trying to run librosa on my raspberry pi 3. After hours of searching through the internet I was finally able to install it but it still throws an error when I try to import it. First, I had problems to install the dependency llvmlite. I finally installed it with the following code: conda install -c numba llvmlite I use python 3.4 build with miniconda. After llvmlite was installed I was able to install librosa with pip (not possible with conda) pi@raspberrypi:~ $ pip install librosa Collecting librosa Using cached https://www.piwheels.hostedpi.com/simple/librosa/librosa- 0.5.1-py3-none-any

python pandas: trying to vectorize a function using date_range

天涯浪子 提交于 2019-11-28 09:49:07
问题 Here is my dataframe: import pandas as pd df = pd.DataFrame({ 'KEY': [1, 2, 3, 1, 1, 2], 'START_DATE': ['2018-01-05', '2018-01-04', '2018-01-01', '2018-01-23', '2018-02-01', '2018-03-11'], 'STOP_DATE': ['2018-01-22', '2018-03-10', '2018-01-31', '2018-02-15', '2018-04-01', '2018-07-21'], 'AMOUNT': [5, 3, 11, 14, 7, 9], }) df.START_DATE = pd.to_datetime(df.START_DATE, format='%Y-%m-%d') df.STOP_DATE = pd.to_datetime(df.STOP_DATE, format='%Y-%m-%d') df >>> AMOUNT KEY START_DATE STOP_DATE 0 5 A

numba,让python速度提升百倍

送分小仙女□ 提交于 2019-11-28 08:06:34
python由于它动态解释性语言的特性,跑起代码来相比java、c++要慢很多,尤其在做科学计算的时候,十亿百亿级别的运算,让python的这种劣势更加凸显。 办法永远比困难多,numba就是解决python慢的一大利器,可以让python的运行速度提升上百倍! 什么是numba? numba是一款可以将python函数编译为机器代码的JIT编译器,经过numba编译的python代码(仅限数组运算),其运行速度可以接近C或FORTRAN语言。 python之所以慢,是因为它是靠CPython编译的,numba的作用是给python换一种编译器。 python、c、numba三种编译器速度对比 使用numba非常简单,只需要将numba装饰器应用到python函数中,无需改动原本的python代码,numba会自动完成剩余的工作。 import numpy as np import numba from numba import jit @jit(nopython=True) # jit,numba装饰器中的一种 def go_fast(a): # 首次调用时,函数被编译为机器代码 trace = 0 # 假设输入变量是numpy数组 for i in range(a.shape[0]): # Numba 擅长处理循环 trace += np.tanh(a[i, i])

Matrix inversion without Numpy

家住魔仙堡 提交于 2019-11-28 07:28:45
I want to invert a matrix without using numpy.linalg.inv . The reason is that I am using Numba to speed up the code, but numpy.linalg.inv is not supported, so I am wondering if I can invert a matrix with 'classic' Python code. With numpy.linalg.inv an example code would look like that: import numpy as np M = np.array([[1,0,0],[0,1,0],[0,0,1]]) Minv = np.linalg.inv(M) Here is a more elegant and scalable solution, imo. It'll work for any nxn matrix and you may find use for the other methods. Note that getMatrixInverse(m) takes in an array of arrays as input. Please feel free to ask any questions

Python numpy: cannot convert datetime64[ns] to datetime64[D] (to use with Numba)

大城市里の小女人 提交于 2019-11-28 06:53:49
I want to pass a datetime array to a Numba function (which cannot be vectorised and would otherwise be very slow). I understand Numba supports numpy.datetime64. However, it seems it supports datetime64[D] (day precision) but not datetime64[ns] (millisecond precision) (I learnt this the hard way: is it documented?). I tried to convert from datetime64[ns] to datetime64[D], but can't seem to find a way! Any ideas? I have summarised my problem with the minimal code below. If you run testdf(mydates) , which is datetime64[D], it works fine. If you run testdf(dates_input) , which is datetime64[ns],

Performance of various numpy fancy indexing methods, also with numba

拟墨画扇 提交于 2019-11-28 06:02:33
Since for my program fast indexing of Numpy arrays is quite necessary and fancy indexing doesn't have a good reputation considering performance, I decided to make a few tests. Especially since Numba is developing quite fast, I tried which methods work well with numba. As inputs I've been using the following arrays for my small-arrays-test: import numpy as np import numba as nb x = np.arange(0, 100, dtype=np.float64) # array to be indexed idx = np.array((0, 4, 55, -1), dtype=np.int32) # fancy indexing array bool_mask = np.zeros(x.shape, dtype=np.bool) # boolean indexing mask bool_mask[idx] =

How to pass additional parameters to numba cfunc passed as LowLevelCallable to scipy.integrate.quad

白昼怎懂夜的黑 提交于 2019-11-28 01:50:36
The documentation discusses using numba's cfunc s as LowLevelCallable argument of scipy.integrate.quad . I need the same thing with additional parameter. I'm basically trying to do something like this: import numpy as np from numba import cfunc import numba.types voidp = numba.types.voidptr def integrand(t, params): a = params[0] # this is additional parameter return np.exp(-t/a) / t**2 nb_integrand = cfunc(numba.float32(numba.float32, voidp))(integrand) However, it does not work, because params are supposed to be voidptr / void* and they cannot be transformed to double . I have the following

Improve Pandas Merge performance

一笑奈何 提交于 2019-11-28 01:05:35
I specifically dont have performace issue with Pands Merge, as other posts suggest, but I've a class in which there are lot of methods, which does a lot of merge on datasets. The class has around 10 group by and around 15 merge. While groupby is pretty fast, out of total execution time of 1.5 seconds for class, around 0.7 seconds goes in those 15 merge calls. I want to speed up performace in those merge calls. As I will have around 4000 iterations, hence saving .5 seconds overall in single iteration will lead to overall performance reduction by around 30min, which will be great. Any