Skip to content

Tree regressors (Random Forest and Gradient Boosting) ​

sci-form ships two deterministic tree regressors for small tabular property models. They are used by the train-random-forest and train-gradient-boosting CLI commands and the matching Rust/Python/WASM functions.

flowchart LR
    A[Feature matrix + targets] --> B[Regression tree]
    B --> C[Random Forest: bootstrap + average]
    B --> D[Gradient Boosting: residual stages]
    C --> E[Predictions + train RMSE]
    D --> E

Random Forest ​

Each tree is trained on a bootstrap sample of the rows. sample_fraction controls the bootstrap size (⌈nf⌉, at least one row), and the prediction is the mean over n_trees trees. Samples not drawn by a tree form its out-of-bag set; RandomForest::oob_score is the mean squared OOB error (None if every sample was in every bag).

Tree growth is controlled by max_depth, min_samples_split, min_samples_leaf and max_features (0 means ⌊d⌋).

Gradient Boosting ​

The model starts from the mean target and fits each tree to the current residuals:

F0=y¯,Ft=Ft−1+ηht,

where η is learning_rate, ht is a regression tree fitted to the residuals on a subsample fraction of rows, and n_estimators is the number of stages. The CLI reports the final training RMSE.

CLI options ​

OptionRandom ForestGradient Boosting
stages/trees--n-trees (≥ 1)--n-estimators (≥ 1)
depth--max-depth (≥ 1)--max-depth (≥ 1)
split--min-samples-split (≥ 2)--min-samples-split (≥ 2)
leaf--min-samples-leaf (≥ 1)--min-samples-leaf (≥ 1)
sampling--sample-fraction in (0, 1]--subsample in (0, 1]
learning rate—--learning-rate > 0
seed--seed (u64)--seed (u64)

Both commands reject empty/ragged feature matrices, mismatched target counts and non-finite values before training. The Python/WASM wrappers also require every count (n_trees/n_estimators, max_depth, min_samples_split, min_samples_leaf) to be a finite non-negative integer; fractional, negative, NaN or infinite counts return ValueError/{"error": ...} instead of being truncated by an unsigned conversion. The checked core applies the tighter per-parameter minima shown above.

Example ​

bash
$ sci-form train-random-forest "[[1,2],[3,4],[5,6],[7,8]]" "[1,2,3,4]"
{
  "model_type": "random_forest",
  "n_estimators": 100,
  "n_samples": 4,
  "predictions": [2.505, 2.505, 2.505, 2.505],
  "train_rmse": 1.118045169033881
}

$ sci-form train-gradient-boosting "[[1,2],[3,4],[5,6],[7,8]]" "[1,2,3,4]"
{
  "model_type": "gradient_boosting",
  "n_estimators": 100,
  "n_samples": 4,
  "predictions": [2.510925865628069, 2.510925865628069, 2.510925865628069, 2.510925865628069],
  "train_rmse": 1.1180873733924923
}

With four samples and min_samples_split = 5, no split is possible, so both models collapse to a constant prediction. This is expected, not a training failure.

Validation and limits ​

  • Internal unit tests cover tree growth, random forest/boosting predictions, cross-validation and deterministic seeds; there is no external scikit-learn parity gate in-tree.
  • No categorical features, missing values, sample weights or monotonic constraints.
  • The models are small-data regressors, not a replacement for a tuned gradient-boosting library. Validate on a held-out set before using predictions quantitatively.
  • Seeds are deterministic (u64); WASM requires a bigint seed.

Surfaces ​

SurfaceEntry point
CLItrain-random-forest, train-gradient-boosting
Rustml::advanced_models::train_random_forest{,_checked}, train_gradient_boosting{,_checked}
Pythontrain_random_forest, train_gradient_boosting
WASMtrain_random_forest, train_gradient_boosting

Released under the MIT License.