Rust Numpy 库 - 按行迭代:无法构建 NpySingleIterBuilder::readwrite returns 行而不是单个值
Rust Numpy library - iterate by rows: unable to build a NpySingleIterBuilder::readwrite that returns rows instead of single values
我正在尝试按行遍历 Numpy 数组。该数组是通过 PyO3 访问的,我认为库对底层 C 对象的访问很好,但我似乎找不到更复杂的 SingleIteratorBuilder 的引用,它可以帮助我按行访问数组。
这是文档页面:https://docs.rs/numpy/0.12.1/numpy/npyiter/struct.NpySingleIterBuilder.html#method.readwrite
(我看到这个项目还处于起步阶段)
这是我编译成 python 模块的 Rust 代码
#[macro_use]
extern crate std;
extern crate ndarray;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use numpy::*;
#[pyfunction]
fn array_print(_py: Python<'_>, matrix1: &PyArray2<i32> ){
let matrix1_iter = NpySingleIterBuilder::readonly(matrix1.readonly()).build().unwrap();
for i in matrix1_iter{
println!("{}", i);
};
}
/// A Python module implemented in Rust.
#[pymodule]
fn algo_match(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(array_print, m)?)?;
Ok(())
}
以及此案例的 python 代码...
#matrixes is the name of the compiled Rust module
import matrixes as am
arr1 = np.array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]], ndmin=2)
am.array_print(arr1)
那returns我刚才说的
~project>python use_algorithm.py
1
2
3
4
5
6
1
2
3
4
5
6
当然,我尝试过使用 ndarray 并将整个 pyArray 复制到一个新对象中,但这是最糟糕的情况,因为我的矩阵在行和列上都应该很大。复制数据可能是更糟糕的选择。
如果数组是连续的,您可以访问.as_slice()
。将矩阵视为一个简单的切片,您可以使用 .chunks(n)
.
遍历行
这只会很容易遍历行。对于列,您可能需要 itertools
.
#[macro_use]
extern crate std;
extern crate ndarray;
use numpy::*;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
#[pyfunction]
fn array_print(_py: Python<'_>, matrix1: &PyArray2<i64>) {
let row_length = matrix1.shape()[1];
for row in matrix1.readonly().as_slice().unwrap().chunks(row_length) {
println!("{:?}", row);
}
}
/// A Python module implemented in Rust.
#[pymodule]
fn matrixes(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(array_print, m)?)?;
Ok(())
}
我正在尝试按行遍历 Numpy 数组。该数组是通过 PyO3 访问的,我认为库对底层 C 对象的访问很好,但我似乎找不到更复杂的 SingleIteratorBuilder 的引用,它可以帮助我按行访问数组。
这是文档页面:https://docs.rs/numpy/0.12.1/numpy/npyiter/struct.NpySingleIterBuilder.html#method.readwrite (我看到这个项目还处于起步阶段)
这是我编译成 python 模块的 Rust 代码
#[macro_use]
extern crate std;
extern crate ndarray;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use numpy::*;
#[pyfunction]
fn array_print(_py: Python<'_>, matrix1: &PyArray2<i32> ){
let matrix1_iter = NpySingleIterBuilder::readonly(matrix1.readonly()).build().unwrap();
for i in matrix1_iter{
println!("{}", i);
};
}
/// A Python module implemented in Rust.
#[pymodule]
fn algo_match(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(array_print, m)?)?;
Ok(())
}
以及此案例的 python 代码...
#matrixes is the name of the compiled Rust module
import matrixes as am
arr1 = np.array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]], ndmin=2)
am.array_print(arr1)
那returns我刚才说的
~project>python use_algorithm.py
1
2
3
4
5
6
1
2
3
4
5
6
当然,我尝试过使用 ndarray 并将整个 pyArray 复制到一个新对象中,但这是最糟糕的情况,因为我的矩阵在行和列上都应该很大。复制数据可能是更糟糕的选择。
如果数组是连续的,您可以访问.as_slice()
。将矩阵视为一个简单的切片,您可以使用 .chunks(n)
.
这只会很容易遍历行。对于列,您可能需要 itertools
.
#[macro_use]
extern crate std;
extern crate ndarray;
use numpy::*;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
#[pyfunction]
fn array_print(_py: Python<'_>, matrix1: &PyArray2<i64>) {
let row_length = matrix1.shape()[1];
for row in matrix1.readonly().as_slice().unwrap().chunks(row_length) {
println!("{:?}", row);
}
}
/// A Python module implemented in Rust.
#[pymodule]
fn matrixes(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(array_print, m)?)?;
Ok(())
}