Skip to content
Jennifer Programming Language

ml - classical machine learning

Enable with use ml;. Classical / predictive machine learning on tabular data, a scikit-learn-lite companion to stats and linalg. Models follow a fit / predict shape: a fit function (ml.kMeans, ml.linearRegression, ...) trains and returns an ml.Model value, and ml.predict / ml.transform apply it. Pure Go stdlib, TinyGo-clean, both binaries.

It is not a deep-learning framework: tensors, autodiff and deep-net training belong to native GPU / C++ tools, and running a pre-trained model is already possible via http / os.run.

jennifer
use ml;
use io;

# Fit a model, then apply it.
def X as list of list of float init [[1.0, 1.0], [2.0, 1.0], [1.0, 2.0], [3.0, 2.0]];
def y as list of float init [6.0, 8.0, 9.0, 13.0];   # y = 2*x1 + 3*x2 + 1
def model as ml.Model init ml.linearRegression($X, $y);
io.printf("%v\n", ml.predict($model, [[5.0, 5.0]]));  # ~[26.0]

Data shape

A feature matrix X is a list of list of float/int (one inner list per sample, one entry per feature); labels / targets y are a list of float/int. Class labels are integers (0, 1, 2, ...); regression targets are any number. ml.predict returns a list of int for a classifier / cluster model and a list of float for a regressor.

Fitting models

Each returns an ml.Model: an opaque, immutable value the garbage collector frees once no copy is left. Copies (assignments, struct fields, list elements, spawned tasks) share the fitted model; a model == itself and its copies, and two separate fits are never equal. The zero value (def m as ml.Model;) holds no model, and every call refuses it.

Fit callKindNotes
ml.linearRegression(X, y)regressionOrdinary least squares (normal equations).
ml.ridge(X, y, alpha)regressionL2-regularized OLS; alpha >= 0 shrinks the coefficients (not the intercept).
ml.lasso(X, y, alpha)regressionL1-regularized (coordinate descent); drives small coefficients to exactly 0.
ml.kNN(X, y, k)classifierk-nearest-neighbours majority vote (Euclidean).
ml.kNNRegressor(X, y, k)regressionk-NN averaging the k nearest targets.
ml.naiveBayes(X, y)classifierGaussian naive Bayes (multiclass).
ml.logisticRegression(X, y) / ml.logisticRegressionWith(X, y, ml.LogisticOptions)classifierBinary or multiclass (one-vs-rest); gradient descent. Only the binary case has predictProba.
ml.decisionTree(X, y) / ml.decisionTreeWith(X, y, ml.TreeOptions)classifierCART with Gini impurity.
ml.decisionTreeRegressor(X, y) / ml.decisionTreeRegressorWith(X, y, ml.TreeOptions)regressionCART with variance-reduction splits, leaf = mean target.
ml.randomForest(X, y) / ml.randomForestWith(X, y, ml.ForestOptions)classifierBagged trees; each split considers a fresh random sqrt(features) subset.
ml.randomForestRegressor(X, y) / ml.randomForestRegressorWith(X, y, ml.ForestOptions)regressionBagged regression trees with the same per-split feature subsets (mean of tree predictions).
ml.kMeans(X, k) / ml.kMeansWith(X, k, ml.KMeansOptions)clusteringLloyd's algorithm, k-means++ seeding.
ml.pca(X, nComponents)transformPrincipal component analysis (covariance eigendecomposition).
ml.standardScaler(X)transformFit a z-score scaler (per-feature mean / stddev).
ml.minMaxScaler(X)transformFit a [0, 1] min-max scaler.

Options

The base form is the With form with zero options. A zero field is the default, a negative one throws ("runtime"), and a count past its ceiling throws "limit".

StructFieldTypeDefaultCeiling
ml.LogisticOptionslearningRatefloat0.1-
epochsint10001000000
ml.TreeOptionsmaxDepthint864
ml.ForestOptionsnTreesint101000
maxDepthint864
ml.KMeansOptionsmaxIterint10010000
jennifer
use ml;
def X as list of list of float init [[0.0], [1.0], [5.0], [6.0]];
def y as list of int init [0, 0, 1, 1];
def f as ml.Model init ml.randomForestWith($X, $y, ml.ForestOptions{nTrees: 50, maxDepth: 4});

Inspecting a fitted model

A model is opaque, but the parameters it learned can be read:

CallApplies toReturns
ml.coefficients(model)linear / ridge / lasso / logisticlist of float (per feature), or list of list of float (per class) for multiclass logistic.
ml.intercept(model)linear / ridge / lasso / logisticfloat, or list of float per class for multiclass logistic.
ml.centroids(model)kMeanslist of list of float (cluster centres).
ml.components(model)pcalist of list of float (principal axes, one row per component).
ml.explainedVariance(model)pcalist of float - the variance ratio each component captures (choose nComponents from these).
ml.featureImportances(model)decisionTree / randomForestlist of float (Gini importances, summing to 1).
jennifer
use ml;
use io;
def m as ml.Model init ml.linearRegression([[1.0, 1.0], [2.0, 1.0], [1.0, 2.0], [3.0, 2.0]], [6.0, 8.0, 9.0, 13.0]);
io.printf("y = %v . x + %v\n", ml.coefficients($m), ml.intercept($m));   # [2, 3], 1

Applying a model

CallReturns
ml.predict(model, X)list of int / list of floatPredicted labels (classifier / cluster) or values (regressor) for each row of X.
ml.transform(model, X)list of list of floatTransformed features (scalers, PCA).
ml.predictProba(model, X)list of floatPositive-class probability, for logisticRegression.

The random models (kMeans, randomForest, trainTestSplit) draw from math's shared random source, so math.randSeed(n) makes a run reproducible. Training needs reproducibility, not secrecy, so ml never uses crypto's unseedable source.

The cost-driving hyper-parameters are bounded; a value above the ceiling is a catchable error, as is a polynomialFeatures product that overflows to a non-finite value.

ParameterCeilingKind
tree maxDepth, forest nTrees, kMeans maxIter, logistic epochssee Options"limit" ("runtime" in the deprecated argument forms)
multiclass logistic classes100"limit"
kFold nSamples * k8388608 (524288 on jennifer-tiny)"limit"
pca features256: each eigen solver sweep costs about 3 x d^3 steps"limit"
confusionMatrix distinct labels2048 (512 on jennifer-tiny): the linalg element limit on the d x d result"limit"
polynomialFeatures degree8"runtime"
polynomialFeatures output width100000"limit"
polynomialFeatures cells (rows x columns)8388608 (524288 on jennifer-tiny)"limit"

ml.pca checks for cancellation once per eigen solver sweep, so task.cancel stops it in a spawned task.

ml.lasso, unlike ordinary least squares, accepts more features than rows (p > n), its feature-selection use case. A very wide polynomialFeatures design is allowed but does dense O(sweeps x features x rows) work; keep the expansion modest. ml.logLoss requires each probability in [0, 1]; an out-of-range value errors instead of being clamped.

jennifer
use ml;
use math;
use io;

math.randSeed(1);
def data as list of list of float init [[0.0, 0.0], [0.2, 0.1], [5.0, 5.0], [5.1, 4.8]];
def km as ml.Model init ml.kMeans($data, 2);
io.printf("clusters: %v\n", ml.predict($km, $data));   # e.g. [0, 0, 1, 1]

# Scale features, then reduce dimensionality.
def scaler as ml.Model init ml.standardScaler($data);
def scaled as list of list of float init ml.transform($scaler, $data);

Deprecated

Each still works and is removed in 0.29.0; jennifer lint reports a call (L301).

DeprecatedUse instead
ml.free(model)nothing: a model is a value the garbage collector frees (ml.free is a no-op)
ml.logisticRegression(X, y, lr[, epochs])ml.logisticRegressionWith(X, y, ml.LogisticOptions{learningRate: ..., epochs: ...})
ml.decisionTree(X, y, maxDepth), ml.decisionTreeRegressor(...)ml.decisionTreeWith(X, y, ml.TreeOptions{maxDepth: ...}), ml.decisionTreeRegressorWith(...)
ml.randomForest(X, y, nTrees[, maxDepth]), ml.randomForestRegressor(...)ml.randomForestWith(X, y, ml.ForestOptions{nTrees: ..., maxDepth: ...}), ml.randomForestRegressorWith(...)
ml.kMeans(X, k, maxIter)ml.kMeansWith(X, k, ml.KMeansOptions{maxIter: ...})

In the deprecated forms an argument must be positive (lr > 0, counts >= 1).

Model selection

CallReturns
ml.trainTestSplit(X, y, testFraction)ml.SplitShuffle and split into {trainX, trainY, testX, testY}; testFraction in (0, 1).
ml.kFold(nSamples, k)list of ml.Foldk contiguous folds, each {trainIdx, testIdx} (index lists into your data).
ml.polynomialFeatures(X, degree)list of list of floatExpand to all monomials up to degree (with a leading bias column); stateless, so the same call fits train and test.
jennifer
export def struct Split { trainX as list of list of float, trainY as list of float, testX as list of list of float, testY as list of float };
export def struct Fold  { trainIdx as list of int, testIdx as list of int };

Metrics

Pure functions over label / value lists (no model). Classification metrics take (yTrue, yPred); precision / recall / f1 take an optional third positive-label argument (default 1).

CallMeaning
ml.accuracy(yTrue, yPred)Fraction of exact matches.
ml.precision(yTrue, yPred [, positive])TP / (TP + FP) for the positive label.
ml.recall(yTrue, yPred [, positive])TP / (TP + FN).
ml.f1(yTrue, yPred [, positive])Harmonic mean of precision and recall.
ml.confusionMatrix(yTrue, yPred)ml.Confusion{labels, matrix} (rows = true, columns = predicted).
ml.rocAuc(yTrue, scores)Binary ROC-AUC from 0 / 1 labels and predicted scores (tie-aware).
ml.logLoss(yTrue, probas)Binary cross-entropy of 0 / 1 labels against predicted probabilities.
ml.rmse(yTrue, yPred) / ml.mse / ml.maeRoot-mean-square / mean-square / mean-absolute error (regression).
ml.r2(yTrue, yPred)Coefficient of determination R^2 (zero-variance targets error).
jennifer
use ml;
use io;
def yt as list of float init [1.0, 0.0, 1.0, 1.0, 0.0, 1.0];
def yp as list of float init [1.0, 0.0, 0.0, 1.0, 0.0, 1.0];
io.printf("accuracy %v, F1 %v\n", ml.accuracy($yt, $yp), ml.f1($yt, $yp, 1));

Strictness and scope

Like the rest of the numeric stack, a degenerate input is a catchable error, not a silent NaN: an empty / ragged matrix, mismatched X / y lengths, a singular regression design (features collinear, also up to rounding), a diverged logistic fit, or magnitudes that overflow the computation all raise a positioned error.

ml targets modest tabular data: thousands of rows, not millions. Large-scale training stays a native-tool job. Support-vector machines, gradient-boosted trees, density-based clustering (DBSCAN), and model serialization are out of scope for this tier.

See also

stats (distributions + inference - the estimate / test companion to ml's fit / predict), linalg (linear algebra), and math. The module index.

Academy talks

TalkWhat it shows
Numbers and data: decimal, stats, linalg, ml, plot and dotregression with two features, k-means and k-nearest neighbours