Findings when accelerating numerical calculations with Python and Numba

I used Numba in my research to speed up Python programs. I stumbled upon various errors during implementation, so I will share that knowledge as a sample.

In my case, the calculation of the particle swarm optimization method using the Runge-Kutta method is 33 times faster, from about 2000 seconds to about 60 seconds.

※Caution※ --Refer to the references for the basic usage of Numba. --All use cases use the nopython mode `` `njit```. --Numba may work without specifying the argument or return type, but here it is assumed that you specify all.

1. Environment

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.2
BuildVersion:	19C57

$ python -V
Python 3.8.5

$ pip freeze
numba==0.51.0
numpy==1.19.1

Numba can be installed with the following command.

$ pip install numba

2. Usage example

2-1. Use np.empty in the function

When using numba, using `` `np.empty``` in a function may result in an error. In that case, specifying the type as follows worked fine.

main.py


import numpy as np
from numba import njit

@njit("f8[:,:]()")
def func():
    x = np.empty((1, 2), dtype=np.float64)
    return x

print(func())

2-2. Returns multiple return values

To return multiple return values, write as `Tuple ((i8, i8))`. Note that the parentheses are doubled.

main.py


import numpy as np
from numba import njit

@njit("Tuple((i8, i8))(i8, i8)")
def func(x, y):
    return x, y

print(func(1, 2))

2-3. Handle multidimensional lists

When dealing with multidimensional lists in Numba, write as `f8 [:,:]`. Since it is two-dimensional, it does not mean that there are two colons, but it seems that two colons are sufficient for any number of dimensions.

main.py


import numpy as np
from numba import njit

@njit("f8[:,:](f8[:,:])")
def func(x):
    return x ** 2

x = np.random.rand(5, 5)
print(func(x))

3. End

There are various ways to speed up Python, such as Cython and Julia, but I think the Numba method, which just writes decorators, is the easiest.

Although there are restrictions such as not being able to use classes and generators, I felt that it would be relatively easy to implement if the method was to speed up the bottleneck locally.

4. References

-Speeding up for statement by numba and argument of jit -Python acceleration Numba introduction 2

Recommended Posts

Findings when accelerating numerical calculations with Python and Numba
I installed and used Numba with Python3.5
Recommended environment and usage when developing with Python
Numerical calculation with Python
Start numerical calculation in Python (with Homebrew and pip)
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Error when playing with python
python with pyenv and venv
Works with Python and R
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python, Selenium and Chromedriver
Scraping with Python and Beautiful Soup
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
[Python] Error and solution memo when using venv with pyenv + anaconda
Reading and writing NetCDF with Python
How to deal with errors when installing Python and pip with choco
Roughly speed up Python with numba
I played with PyQt5 and Python3
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
When matplotlib doesn't work with python2.7
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Sugoroku game and addition game with python
FM modulation and demodulation with Python
[Python] Format when to_csv with pandas
I replaced the numerical calculation of Python with Rust and compared the speed
Bilinear interpolation function when performing nonlinear coordinate conversion with Python and Numpy
Settings when using Python 3 requests and Beautiful Soup with crostini on Chromebook
When I screenfetch with xonsh, python x.x comes out and it's sad
Communicate between Elixir and Python with gRPC
Calculate and display standard weight with python
Monitor Mojo outages with Python and Skype
Snippet when searching all bits with python
[Python] Use and and or when creating variables
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Passwordless authentication with RDS and IAM (Python)
Using Python and MeCab with Azure Databricks
Note when creating an environment with python
POST variously with Python and receive with Flask
Capturing images with Pupil, python and OpenCV
Precautions when solving DP problems with Python
Fractal to make and play with Python
A memo with Python2.7 and Python3 on CentOS
Use PIL and Pillow with Cygwin Python
Create and decrypt Caesar cipher with python