Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Errors

Most model and data utility methods return Result<T, rustyml::error::Error>. Use ? to return an error to the caller. Use match when the caller can correct a specific problem.

Handle a model state error

This program handles prediction before training. It then trains the model and repeats the prediction.

use ndarray::array;
use rustyml::error::{Error, RustymlResult};
use rustyml::machine_learning::LinearRegression;

fn main() -> RustymlResult<()> {
    let x = array![[1.0], [2.0]];
    let mut model = LinearRegression::new(true);
    match model.predict(&x) {
        Err(Error::NotFitted(_)) => println!("fit the model before prediction"),
        Err(error) => return Err(error),
        Ok(_) => println!("prediction succeeded"),
    }
    model.fit(&x, &array![3.0, 5.0])?;
    println!("prediction: {:.1}", model.predict(&array![[3.0]])?[0]);
    Ok(())
}

Output:

fit the model before prediction
prediction: 7.0

Error is non-exhaustive. Include a fallback arm when you match its variants.

Correct common errors

ErrorAction
EmptyInputSupply a nonempty array.
DimensionMismatchCheck sample counts and feature counts.
ShapeMismatchCheck the complete tensor shape.
InvalidParameterCheck the constructor or method parameters.
InvalidInputCheck array ranks, label values, and other input requirements.
NonFiniteCheck for NaN, infinity, or numerical overflow.
NotFittedCall fit before prediction or transformation.
NotConvergedCheck data scaling and the selected solver settings.
NeuralNetwork(NnError::NotCompiled(_))Call compile before training or evaluation.
IoCheck the file path, permissions, and file format.

NnError belongs to rustyml::neural_network. The neural network feature enables the NeuralNetwork error variant. Each method documents the errors that it can return.

Metric input checks

Metrics return scores directly. Invalid metric inputs can cause a panic. Check array lengths, label values, and metric requirements before you call them. See Evaluate models for common input requirements.