Crate rhai_ml

source ·
Expand description

tests Crates.io docs.rs

§About rhai-ml

This crate provides some basic machine learning and artificial intelligence utilities for the Rhai scripting language. For a complete API reference, check the docs.

§Install

To use the latest released version of rhai-ml, add this to your Cargo.toml:

rhai-ml = "0.1.2"

To use the bleeding edge instead, add this:

rhai-ml = { git = "https://github.com/cmccomb/rhai-ml" }

§Usage

Using this crate is pretty simple! If you just want to evaluate a single line of Rhai, then you only need:

use rhai::FLOAT;
use rhai_ml::eval;
let result = eval::<FLOAT>("\
let xdata = [[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]; \
let ydata = [1.0, 2.0, 3.0]; \
let model = train(xdata, ydata, \"linear\"); \
let ypred = predict(xdata, model);
ypred[0]
").unwrap();

If you need to use rhai-ml as part of a persistent Rhai scripting engine, then do this instead:

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

// Create a new Rhai engine
let mut engine = Engine::new();

// Add the rhai-ml package to the new engine
engine.register_global_module(MLPackage::new().as_shared_module());

// Now run your code
let value = engine.eval::<FLOAT>("\
let xdata = [[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]; \
let ydata = [1.0, 2.0, 3.0]; \
let model = train(xdata, ydata, \"linear\"); \
let ypred = predict(xdata, model);
ypred[0]
").unwrap();

§Features

FeatureDefaultDescription
metadataDisabledEnables exporting function metadata and is necessary for running doc-tests on Rhai examples.

§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

Uses a smartcore machine learning model (trained with the train function to predict dependent variables.

let xdata = [[1.0, 2.0],
             [2.0, 3.0],
             [3.0, 4.0]];
let ydata = [1.0, 2.0, 3.0];
let model = train(xdata, ydata, "linear");
let ypred = predict(xdata, model);
true

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

Trains a smartcore machine learning model. The model can then be used to make predictions with the predict function Available model types are:

  1. linear - ordinary least squares linear regression
  2. logistic - logistic regression
  3. lasso - lasso regression
let xdata = [[1.0, 2.0],
             [2.0, 3.0],
             [3.0, 4.0]];
let ydata = [1.0, 2.0, 3.0];
let model = train(xdata, ydata, "linear");
true;

Structs§

Functions§

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