Skip to main content

ModelSave

Trait ModelSave 

Source
pub trait ModelSave: Serialize {
    // Required methods
    fn save_with_name(
        &self,
        path: &str,
        name: Option<String>,
    ) -> Result<(), Error>;
    fn model_type() -> ModelType;

    // Provided method
    fn save(&self, path: &str) -> Result<(), Error> { ... }
}
Expand description

Trait for saving models to disk.

This trait is implemented by all regression result types that support serialization. Models are saved as JSON with a metadata wrapper.

§Example

let y = vec![2.0, 4.0, 6.0, 8.0];
let x1 = vec![1.0, 2.0, 3.0, 4.0];
let names = vec!["Intercept".into(), "X1".into()];

let model = ols_regression(&y, &[x1], &names).unwrap();
model.save("my_model.json").unwrap();

Required Methods§

Source

fn save_with_name(&self, path: &str, name: Option<String>) -> Result<(), Error>

Save the model to a file with a custom name.

The name is stored in the model metadata and can be used to identify the model later.

§Arguments
  • path - File path to save to
  • name - Optional custom name for the model
Source

fn model_type() -> ModelType

Get the model type identifier.

This is used when serializing to store the model type in metadata.

Provided Methods§

Source

fn save(&self, path: &str) -> Result<(), Error>

Save the model to a file.

The file will contain JSON with metadata (format version, model type, timestamp) and the model data.

§Arguments
  • path - File path to save to (will be created/overwritten)
§Returns

Returns Ok(()) on success, or an Error if serialization or file I/O fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§