onnx-export-rs 0.1.1

Export canonical Rust machine-learning models to ONNX
Documentation
use ndarray::Array2;

use crate::{Error, Result};

/// Nearest-centroid inference state, including fitted k-means centroids.
#[derive(Clone, Debug, PartialEq)]
pub struct CentroidModel {
    /// Matrix shaped `[centroids, input_features]`.
    pub centroids: Array2<f64>,
}

impl CentroidModel {
    /// Creates validated centroid state.
    ///
    /// # Errors
    ///
    /// Returns an error for an empty or non-finite matrix.
    pub fn new(centroids: Array2<f64>) -> Result<Self> {
        if centroids.nrows() == 0
            || centroids.ncols() == 0
            || centroids.iter().any(|value| !value.is_finite())
        {
            return Err(Error::InvalidModel(
                "centroids must be non-empty and finite".into(),
            ));
        }
        Ok(Self { centroids })
    }

    /// Required input feature count.
    #[must_use]
    pub fn n_features(&self) -> usize {
        self.centroids.ncols()
    }
}