https://github.com/rhaiscript/rhai-ml
Machine learning for Rhai.
https://github.com/rhaiscript/rhai-ml
artificial-intelligence lib machine-learning rhai rhai-ml scripting-language
Last synced: 8 months ago
JSON representation
Machine learning for Rhai.
- Host: GitHub
- URL: https://github.com/rhaiscript/rhai-ml
- Owner: rhaiscript
- License: apache-2.0
- Created: 2023-04-09T15:50:56.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-02-09T16:35:10.000Z (about 2 years ago)
- Last Synced: 2025-06-11T07:56:48.044Z (9 months ago)
- Topics: artificial-intelligence, lib, machine-learning, rhai, rhai-ml, scripting-language
- Language: Rust
- Homepage: https://crates.io/crates/rhai-ml
- Size: 21.5 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE.txt
Awesome Lists containing this project
README
[](https://github.com/rhaiscript/rhai-ml/actions/workflows/tests.yml)
[](https://crates.io/crates/rhai-ml)
[](https://docs.rs/rhai-ml)
# About `rhai-ml`
This crate provides some basic machine learning and artificial intelligence utilities for the [`Rhai`](https://rhai.rs/)
scripting language. For a complete API reference, check [the docs](https://docs.rs/rhai-ml).
# Install
To use the latest released version of `rhai-ml`, add this to your `Cargo.toml`:
```toml
rhai-ml = "0.1.2"
```
To use the bleeding edge instead, add this:
```toml
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`](https://rhai.rs/), then you only need:
```rust
use rhai::FLOAT;
use rhai_ml::eval;
let result = eval::("\
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`](https://rhai.rs/) scripting engine, then do this instead:
```rust
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::("\
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
| Feature | Default | Description |
| ----------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `metadata` | Disabled | Enables exporting function metadata and is ___necessary for running doc-tests on Rhai examples___. |