[Rust / Python] Handle numpy with PyO3 (August 2020 version)

This is a code example that calls myfunction (x: f64)-> Array2 <f64> implemented in Rust from Python. Since it is a zero copy, it is recommended when large data is generated on the Rust side and analyzed by Python. Frequently used So for copy and paste.

Cargo.toml


[lib]
name = "mypackage"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.11", features = ["extension-module"] }
ndarray = "0.13"
numpy = "0.11"

src/lib.rs


use pyo3::prelude::*;
use ndarray::Array2;
use numpy::{IntoPyArray, PyArray2};

#[pymodule]
fn mypackage(_py: Python, m: &PyModule) -> PyResult<()> {
    #[pyfn(m, "myfunction")]
    fn myfunction_py<'py>(py: Python<'py>, x: f64) -> &'py PyArray2<f64> {
        let arr = myfunction(x);

        arr.into_pyarray(py)
    }

    Ok(())
}

Compile and run example:

$ cargo build --release
$ ln -s ./target/release/libmypackage.so mypackage.so
$ 
$ python3
>>> import mypackage
>>> x = 3.14
>>> arr = mypackage.myfunction(x)

Recommended Posts

[Rust / Python] Handle numpy with PyO3 (August 2020 version)
Handle Excel with python
Handle rabbimq with python
Extend NumPy with Rust
Check version with python
[Python] Calculation method with numpy
[Tips] Handle Athena with Python
Handle numpy arrays with f2py
Specify python version with virtualenv
Python3 | Getting Started with numpy
Handle Base91 keys with python + redis.
Easily handle lists with python + sqlite3
Self-organizing map in Python NumPy version
Ideone> Python version: 3.5 (as of August 29, 2017)
Handle Excel CSV files with Python
Easily handle databases with Python (SQLite3)
Manage each Python version with Homebrew
[Python Windows] pip install with Python version
Trying to handle SQLite3 with Python [Note]
Handle numpy with Cython (method by memoryview)
[Translation] Getting Started with Rust for Python Programmers
Manage Python multiple version environment with Pythonz, virtualenv
Debug with VS Code using boost python numpy
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Using Intel MKL with NumPy / SciPy (November 2019 version)
Handle zip files with Japanese filenames in Python 3
[Improved version] Script to monitor CPU with Python
I tried running faiss with python, Go, Rust
[Python] How to handle Japanese characters with openCV
Solve "AtCoder version! Ant book (beginner)" with Python!
My Numpy (Python)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
#Python basics (#Numpy 1/2)
with syntax (Python)
#Python basics (#Numpy 2/2)
Bingo with python
Zundokokiyoshi with python
PYTHON2.7 64bit version
Python #Numpy basics
Excel with Python
[Python] Numpy memo
Microcomputer with Python
Cast with python
Text mining with Python ① Morphological analysis (re: Linux version)
[Python] Create structured array (store heterogeneous data with NumPy)
Easy scraping with Python (JavaScript / Proxy / Cookie compatible version)
I want to handle optimization with python and cplex
Understanding with mathematical formulas and Python LiNGAM (ICA version)
Version control of Node, Ruby and Python with anyenv