Skip to main content

Crate rhai_ml

Crate rhai_ml 

Source
Expand description

tests Crates.io docs.rs

§rhai-ml

Machine learning for the Rhai scripting language, backed by SmartCore. Train linear, lasso, and logistic regression models directly in your scripts, with optional regularization settings.

§Install

rhai-ml = "0.1.4"

§Usage

use rhai_ml::eval;

let prediction = eval::<f64>(r#"
    let model = train([[0], [1], [2], [3]], [1, 3, 5, 7], "linear");
    predict([[4]], model)[0]
"#).unwrap();

assert!((prediction - 9.0).abs() < 0.000001);

To add the package to an existing Rhai engine:

use rhai::{packages::Package, Engine};
use rhai_ml::MLPackage;

let mut engine = Engine::new();
engine.register_global_module(MLPackage::new().as_shared_module());

Inputs must be nonempty rectangular arrays of finite numbers, with one target per training row. Logistic regression uses integer class labels. Invalid inputs return Rhai errors that scripts can handle with try/catch.

The optional metadata feature generates API documentation and tests the Rhai examples. Run cargo run --example quickstart to try all three algorithms.

See the API reference, input rules and errors, development checks, and changelog.

§API

This package provides a large variety of functions to help with machine learning and artificial intelligence: predict   train  

§predict(x: Array, model: Model) -> Array

Predicts dependent variables with a model produced by train. x must be a nonempty rectangular array of finite numbers with the same number of features as the training data. Regression returns floats; logistic regression returns integer class labels.

let model = train([[0], [1], [2], [3]], [1, 3, 5, 7], "linear");
let ypred = predict([[4], [5]], model);
abs(ypred[0] - 9.0) < 0.000001 && abs(ypred[1] - 11.0) < 0.000001;

§train

§train(x: Array, y: Array, algorithm: String) -> Model

Trains a smartcore machine learning model. Use predict to make predictions. Available model types are:

  1. linear - ordinary least squares linear regression
  2. logistic - logistic regression with integer class labels
  3. lasso - lasso regression

x must be a nonempty rectangular array of finite numbers. Regression targets may be integers or floats; logistic targets must be integers. The number of targets must equal the number of rows in x. Linear and lasso regression require more rows than features. Input validation and backend errors are reported as Rhai errors.

let xdata = [[0.0], [1.0], [2.0], [3.0]];
let ydata = [1.0, 3.0, 5.0, 7.0];
let model = train(xdata, ydata, "linear");
let ypred = predict([[4.0]], model);
abs(ypred[0] - 9.0) < 0.000001;

§train(x: Array, y: Array, algorithm: String, options: Map) -> Model

Trains a model with an options map. Lasso and logistic regression accept alpha, a finite nonnegative integer or float controlling regularization. Higher values apply a stronger penalty. The defaults are 1.0 for lasso and 0.0 for logistic regression. Omitting alpha, or passing an empty map, preserves the defaults of the three-argument train call.

Linear regression accepts only an empty map. Unknown options and invalid values return Rhai errors. The input requirements are the same as for the three-argument call.

let x = [[0], [1], [2], [3], [4], [5]];
let y = [1, 3, 5, 7, 9, 11];
let model = train(x, y, "lasso", #{ alpha: 0.1 });
abs(predict([[6]], model)[0] - 13.0) < 0.2;

Structs§

MLPackage
Package for machine learning

Functions§

eval
This provides the ability to easily evaluate a line (or lines) of code without explicitly setting up a script engine