I'm trying to pass a list of lists from Python to Rust using Py03. The function I'm trying to pass it to has this signature:
pub fn k_nearest_neighbours(k: usize, x: &[[f32; 2]], y: &[[f32; 3]]) -> Vec<Option<f32>>
I'm writing bindings for a pre-existing lib, therefore I cannot alter the original code. My current way of doing things is this:
// This is example code == DOES NOT WORK
#[pyfunction] // make a new function within a new library with pyfunction macro
fn k_nearest_neighbours(k: usize, x: Vec<Vec<f32>>, y: Vec<f32>) -> Vec<Option<f32>> {
// reformat input where necessary
let x_slice = x.as_slice();
// return original lib's function return
classification::k_nearest_neighbours(k, x_slice, y)
}
The x.as_slice() function almost does what I need it to, it gives me a slice of vectors &[Vec<f32>], instead of a slice of slices &[[f32; 3]].
I want to be able to run this Python code:
from rust_code import k_nearest_neighbours as knn # this is the Rust function compiled with PyO3
X = [[0.0, 1.0], [2.0, 3.0], [4.0, 5.0], [0.06, 7.0]]
train = [
[0.0, 0.0, 0.0],
[0.5, 0.5, 0.0],
[3.0, 3.0, 1.0],
[4.0, 3.0, 1.0],
]
k = 2
y_true = [0, 1, 1, 1]
y_test = knn(k, X, train)
assert(y_true == y_test)