Standard library · Machine learning

ml

Classic machine learning: preprocessing, regression, classification, trees, clustering and evaluation.

import ml

This package gives you the classic machine learning algorithms behind an interface much like scikit-learn. The data is a matrix where each row is a sample and each column a feature, and the labels are a one-column matrix. Every model is a struct: build it with its parameters, train it with fit and use it with predict.

Before training, prepare the data with StandardScaler and SimpleImputer and split it with TrainTestSplit; after that, measure the model with metrics such as Accuracy, F1 and R2Score, and pick the parameters with GridSearch. For neural networks see nn.

Types

Split

struct Split
Data after a split.
NameTypeDescription
x_trainmatrix.MatrixThe training features.
x_testmatrix.MatrixThe test features.
y_trainmatrix.MatrixThe training labels.
y_testmatrix.MatrixThe test labels.
ml-Prep.salam
import ml
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(matrix.Chop(m, 0.000001), 3)
end

func ints(v: Vector<int>): str:
    mut text := "["
    repeat v.len() in i:
        if i > 0:
            text += " "
        end
        text += "" + v.get(i)
    end
    ret text + "]"
end

func main:
    mut data := build(4, 2, "1 10 2 20 3 30 4 40")
    mut standard := ml.StandardScaler {}
    mut scaled := standard.fit_transform(data)
    show("standard", scaled)
    show("mean", standard.mean)
    println standard.ok, standard.with_mean, standard.with_std, matrix.ShapeString(standard.std)
    show("back", standard.inverse_transform(scaled))
    mut minmax := ml.MinMaxScaler { lo = 0.0, hi = 1.0 }
    println minmax.fit(data)
    show("minmax", minmax.transform(data))
    show("back", minmax.inverse_transform(minmax.fit_transform(data)))
    println minmax.ok, matrix.ShapeString(minmax.data_min), matrix.ShapeString(minmax.data_range)
    println standard.fit(data), matrix.ShapeString(standard.transform(data))

    mut labels := Vector {} as Vector<str>
    each label in ["cat", "dog", "cat", "fish"]:
        labels.push(label)
    end
    mut encoder := ml.LabelEncoder {}
    mut codes := encoder.fit_transform(labels)
    show("label", codes)
    println encoder.ok, encoder.classes.len(), encoder.index_of("dog"), encoder.inverse_transform(codes).get(3), encoder.fit(labels), matrix.ShapeString(encoder.transform(labels))
    mut onehot := ml.OneHotEncoder {}
    mut hot := onehot.fit_transform(codes)
    show("onehot", hot)
    show("back", onehot.inverse_transform(hot))
    println onehot.ok, onehot.n_classes, onehot.fit(codes), matrix.ShapeString(onehot.transform(codes))
    println ints(ml.LabelsToInts(codes)), ml.CountClasses(codes)

    mut missing := build(3, 2, "1 4 nan 6 3 nan")
    mut imputer := ml.SimpleImputer { strategy = ml.IMPUTE_MEAN }
    show("mean", imputer.fit_transform(missing))
    mut imputer2 := ml.SimpleImputer { strategy = ml.IMPUTE_CONSTANT, fill = -1.0 }
    println imputer2.fit(missing), imputer2.ok, matrix.ShapeString(imputer2.stats)
    show("constant", imputer2.transform(missing))
    println ml.IMPUTE_MEDIAN
    mut poly := ml.PolynomialFeatures { degree = 2, include_bias = true }
    show("polynomial", poly.transform(build(1, 2, "2 3")))

    mut linear := build(4, 2, "1 2 2 4 3 6 4 8")
    mut pca := ml.PCA { n_components = 1 }
    println pca.fit(linear), pca.ok, matrix.ShapeString(pca.components), matrix.ShapeString(pca.mean)
    show("variance", pca.explained_ratio)
    println matrix.ShapeString(pca.explained_variance), matrix.ShapeString(pca.transform(linear)), matrix.ShapeString(pca.fit_transform(linear))
    show("back", pca.inverse_transform(pca.transform(linear)))
    mut svd := ml.TruncatedSVD { n_components = 1 }
    println svd.fit(linear), svd.ok, matrix.ShapeString(svd.components), matrix.ShapeString(svd.singular_values), matrix.ShapeString(svd.explained_variance), matrix.ShapeString(svd.explained_ratio)
    println matrix.ShapeString(svd.transform(linear)), matrix.ShapeString(svd.fit_transform(linear))

    mut x := build(6, 1, "1 2 3 4 5 6")
    mut y := build(6, 1, "0 0 0 1 1 1")
    mut split := ml.TrainTestSplit(x, y, 0.5, 7)
    println matrix.ShapeString(split.x_train), matrix.ShapeString(split.x_test), matrix.ShapeString(split.y_train), matrix.ShapeString(split.y_test)
    mut folds := ml.KFold(6, 3, false, 0)
    println folds.count(), ints(folds.test.get(0)), ints(folds.train.get(0))
    mut stratified := ml.StratifiedKFold(y, 3, 1)
    println stratified.count(), stratified.test.get(0).len()
    println ml.ML_TOL > 0.0
    standard.free()
    minmax.free()
    encoder.free()
    imputer.free()
    pca.free()
    svd.free()
end
Outputstandard [-1.342,-1.342; -0.447,-0.447; 0.447,0.447; 1.342,1.342] mean [2.500,25.000] true true true 1x2 back [1.000,10.000; 2.000,20.000; 3.000,30.000; 4.000,40.000] true minmax [0.000,0.000; 0.333,0.333; 0.667,0.667; 1.000,1.000] back [1.000,10.000; 2.000,20.000; 3.000,30.000; 4.000,40.000] true 1x2 1x2 true 4x2 label [0.000; 1.000; 0.000; 2.000] true 3 1 fish true 4x1 onehot [1.000,0.000,0.000; 0.000,1.000,0.000; 1.000,0.000,0.000; 0.000,0.000,1.000] back [0.000; 1.000; 0.000; 2.000] true 3 true 4x3 [0 1 0 2] 3 mean [1.000,4.000; 2.000,6.000; 3.000,5.000] true true 1x2 constant [1.000,4.000; -1.000,6.000; 3.000,-1.000] 1 polynomial [1.000,2.000,3.000,4.000,6.000,9.000] true true 2x1 1x2 variance [1.000] 1x1 4x1 4x1 back [1.000,2.000; 2.000,4.000; 3.000,6.000; 4.000,8.000] true true 2x1 1x1 1x1 1x1 4x1 4x1 3x1 3x1 3x1 3x1 3 [0 1] [2 3 4 5] 3 2 true

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

Folds

struct Folds
Cross-validation folds.
NameTypeDescription
trainVector>The training row numbers of each fold.
testVector>The test row numbers of each fold.

The example for this item is shown with the ones above: ml-Prep.salam

count

func count(): int
The number of folds.

The example for this item is shown with the ones above: ml-Prep.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

GridResult

struct GridResult
The result of a grid search.
NameTypeDescription
scoresVectorThe mean score of each candidate.
bestint = -1The number of the best one.
best_scoref64 = 0The best score.
ml-Select.salam
import ml
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func f3(number: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, number), 3)
end

func neighbors(k: int, train: matrix.Matrix, labels: matrix.Matrix, test: matrix.Matrix): matrix.Matrix:
    mut model := ml.KNNClassifier { k = k }
    _ := model.fit(train, labels)
    pred := model.predict(test)
    model.free()
    ret pred
end

func main:
    mut x := build(8, 2, "1 1 1 2 2 1 2 2 6 6 6 7 7 6 7 7")
    mut y := build(8, 1, "0 0 0 0 1 1 1 1")
    mut folds := ml.StratifiedKFold(y, 4, 1)
    mut scores := ml.CrossValScore((a: matrix.Matrix, b: matrix.Matrix, c: matrix.Matrix): ret neighbors(1, a, b, c) end, (a: matrix.Matrix, b: matrix.Matrix): ret ml.Accuracy(a, b) end, x, y, folds)
    println scores.len(), f3(ml.MeanScore(scores))
    mut parallel := ml.CrossValScoreParallel((a: matrix.Matrix, b: matrix.Matrix, c: matrix.Matrix): ret neighbors(1, a, b, c) end, (a: matrix.Matrix, b: matrix.Matrix): ret ml.Accuracy(a, b) end, x, y, folds, 2)
    println f3(ml.MeanScore(parallel))

    mut candidates := Vector {} as Vector<ml.Candidate>
    candidates.push(ml.Candidate { name = "k=1", fit = (a: matrix.Matrix, b: matrix.Matrix, c: matrix.Matrix): ret neighbors(1, a, b, c) end })
    candidates.push(ml.Candidate { name = "k=3", fit = (a: matrix.Matrix, b: matrix.Matrix, c: matrix.Matrix): ret neighbors(3, a, b, c) end })
    mut result := ml.GridSearch(candidates, (a: matrix.Matrix, b: matrix.Matrix): ret ml.Accuracy(a, b) end, x, y, folds, 1)
    println result.scores.len(), result.best >= 0, f3(result.best_score), candidates.get(result.best).name.len() > 0
end
Output4 1.000 1.000 2 true 1.000 true

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Select.salam

Candidate

struct Candidate
One candidate in a grid search.
NameTypeDescription
namestr = ""The name.
fitfunc(matrix.Matrix, matrix.Matrix, matrix.Matrix): matrix.MatrixA function that takes the training and test data and returns a prediction.

The example for this item is shown with the ones above: ml-Select.salam

KNNClassifier

struct KNNClassifier
A k nearest neighbours classifier.
NameTypeDescription
kint = 5The number of neighbours or clusters.
weightedbool = falseWeights the neighbours by inverse distance.
algorithmintThe search method, one of the KNN_... constants.
leaf_sizeint = 16The leaf size of the k-d tree.
workersint = 0The number of threads; zero means automatic.
xtrainmatrix.MatrixThe training data.
ytrainmatrix.MatrixThe training labels.
treeKDTreeThe k-d tree.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.
ml-Classify.salam
import ml
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(matrix.Chop(m, 0.000001), 3)
end

func f3(number: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, number), 3)
end

func main:
    mut x := build(8, 2, "1 1 1 2 2 1 2 2 6 6 6 7 7 6 7 7")
    mut y := build(8, 1, "0 0 0 0 1 1 1 1")
    mut test := build(2, 2, "1.5 1.5 6.5 6.5")

    mut knn := ml.KNNClassifier { k = 3, weighted = true, algorithm = ml.KNN_KDTREE }
    println knn.fit(x, y), knn.ok, knn.n_classes, knn.leaf_size, knn.workers, matrix.ShapeString(knn.xtrain), matrix.ShapeString(knn.ytrain), knn.tree.ok
    show("neighbors", knn.predict(test))
    println matrix.ShapeString(knn.predict_proba(test)), f3(knn.score(x, y)), ml.KNN_BRUTE
    mut regressor := ml.KNNRegressor { k = 2 }
    println regressor.fit(x, y), regressor.ok, regressor.weighted, regressor.algorithm, regressor.leaf_size, regressor.workers, matrix.ShapeString(regressor.xtrain), matrix.ShapeString(regressor.ytrain), regressor.tree.ok
    show("regressor", regressor.predict(test))
    println f3(regressor.score(x, y))

    mut gaussian := ml.GaussianNB {}
    println gaussian.fit(x, y), gaussian.ok, gaussian.n_classes, gaussian.var_smoothing > 0.0, matrix.ShapeString(gaussian.means), matrix.ShapeString(gaussian.vars), matrix.ShapeString(gaussian.log_priors)
    show("gaussian", gaussian.predict(test))
    println matrix.ShapeString(gaussian.predict_proba(test)), f3(gaussian.score(x, y))
    mut counts := build(4, 3, "3 0 1 2 0 0 0 3 1 0 2 2")
    mut topic := build(4, 1, "0 0 1 1")
    mut multinomial := ml.MultinomialNB { alpha = 1.0 }
    println multinomial.fit(counts, topic), multinomial.ok, multinomial.fit_prior, multinomial.n_classes, matrix.ShapeString(multinomial.log_prob), matrix.ShapeString(multinomial.log_priors)
    show("multinomial", multinomial.predict(counts))
    println matrix.ShapeString(multinomial.decision(counts)), matrix.ShapeString(multinomial.predict_proba(counts)), f3(multinomial.score(counts, topic))

    mut lda := ml.LDA {}
    println lda.fit(x, y), lda.ok, lda.n_classes, lda.ridge > 0.0, matrix.ShapeString(lda.means), matrix.ShapeString(lda.cov), matrix.ShapeString(lda.log_priors)
    show("linear", lda.predict(test))
    println matrix.ShapeString(lda.predict_proba(test)), f3(lda.score(x, y))
    mut qda := ml.QDA {}
    println qda.fit(x, y), qda.ok, qda.n_classes, qda.ridge > 0.0, matrix.ShapeString(qda.means), qda.covs.len(), matrix.ShapeString(qda.log_priors)
    show("quadratic", qda.predict(test))
    println matrix.ShapeString(qda.predict_proba(test)), f3(qda.score(x, y))

    mut svm := ml.LinearSVM { c = 1.0 }
    println svm.fit(x, y), svm.ok, svm.n_classes, svm.max_iter, svm.tol > 0.0, svm.seed, matrix.ShapeString(svm.coef), matrix.ShapeString(svm.intercept)
    show("linear svm", svm.predict(test))
    println matrix.ShapeString(svm.decision(test)), f3(svm.score(x, y))
    mut rbf := ml.SVC { kernel = ml.KERNEL_RBF, c = 10.0, gamma = 0.5 }
    println rbf.fit(x, y), rbf.ok, rbf.n_classes, rbf.n_support() > 0, rbf.degree, rbf.coef0, rbf.max_passes, rbf.tol > 0.0, rbf.seed
    println matrix.ShapeString(rbf.support), matrix.ShapeString(rbf.dual), matrix.ShapeString(rbf.bias)
    show("rbf kernel", rbf.predict(test))
    println matrix.ShapeString(rbf.decision(test)), f3(rbf.score(x, y)), ml.KERNEL_LINEAR, ml.KERNEL_POLY

    mut truth := build(6, 1, "0 0 1 1 1 0")
    mut pred := build(6, 1, "0 1 1 1 0 0")
    mut score := build(6, 1, "0.1 0.6 0.8 0.9 0.4 0.2")
    println f3(ml.Accuracy(truth, pred)), f3(ml.Precision(truth, pred, 1.0)), f3(ml.Recall(truth, pred, 1.0)), f3(ml.F1(truth, pred, 1.0)), f3(ml.MacroF1(truth, pred, 2))
    show("confusion", ml.ConfusionMatrix(truth, pred, 2))
    println f3(ml.RocAuc(truth, score)), f3(ml.AveragePrecision(truth, score)), f3(ml.BalancedAccuracy(truth, pred)), f3(ml.MatthewsCorrCoef(truth, pred)), f3(ml.CohenKappa(truth, pred))
    println matrix.ShapeString(ml.RocCurve(truth, score)), matrix.ShapeString(ml.PrecisionRecallCurve(truth, score))
    mut proba := build(6, 2, "0.9 0.1 0.4 0.6 0.2 0.8 0.1 0.9 0.6 0.4 0.8 0.2")
    println f3(ml.LogLoss(truth, proba))
    knn.free()
    regressor.free()
    gaussian.free()
    multinomial.free()
    lda.free()
    qda.free()
    svm.free()
    rbf.free()
end
Outputtrue true 2 16 0 8x2 8x1 true neighbors [0.000; 1.000] 2x2 1.000 0 true true false 0 16 0 8x2 8x1 false regressor [0.000; 1.000] 1.000 true true 2 true 2x2 2x2 2x1 gaussian [0.000; 1.000] 2x2 1.000 true true true 2 2x3 2x1 multinomial [0.000; 0.000; 1.000; 1.000] 4x2 4x2 1.000 true true 2 true 2x2 2x2 2x1 linear [0.000; 1.000] 2x2 1.000 true true 2 true 2x2 2 2x1 quadratic [0.000; 1.000] 2x2 1.000 true true 2 1000 true 42 2x1 1x1 linear svm [0.000; 1.000] 2x1 1.000 true true 2 true 3 1 20 true 42 8x2 8x1 1x1 rbf kernel [0.000; 1.000] 2x1 1.000 0 2 0.667 0.667 0.667 0.667 0.667 confusion [2.000,1.000; 1.000,2.000] 0.889 0.917 0.667 0.333 0.333 7x3 6x3 0.415

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Select.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

KNNRegressor

struct KNNRegressor
A k nearest neighbours regressor.
NameTypeDescription
kint = 5The number of neighbours or clusters.
weightedbool = falseWeights the neighbours by inverse distance.
algorithmintThe search method, one of the KNN_... constants.
leaf_sizeint = 16The leaf size of the k-d tree.
workersint = 0The number of threads; zero means automatic.
xtrainmatrix.MatrixThe training data.
ytrainmatrix.MatrixThe training labels.
treeKDTreeThe k-d tree.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

GaussianMixture

struct GaussianMixture
A Gaussian mixture model, fitted with EM.
NameTypeDescription
kint = 2The number of neighbours or clusters.
max_iterint = 100The maximum number of iterations.
tolf64 = 0.0001The convergence tolerance.
var_floorf64 = 1e-06The minimum variance.
seedi64 = 42The random seed, for a repeatable result.
meansmatrix.MatrixThe mean of each class.
varsmatrix.MatrixThe variances.
weightsmatrix.MatrixThe weight of each component.
log_likelihoodf64 = 0The log likelihood.
n_iterint = 0The number of iterations run.
okbool = falseWhether the fit succeeded.
ml-Cluster.salam
import ml
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func f3(number: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, number), 3)
end

func main:
    mut x := build(8, 2, "1 1 1 2 2 1 2 2 8 8 8 9 9 8 9 9")
    mut truth := build(8, 1, "0 0 0 0 1 1 1 1")

    mut km := ml.KMeans { k = 2, seed = 1 }
    mut labels := km.fit_predict(x)
    println km.ok, km.n_iter > 0, f3(km.inertia), matrix.ShapeString(km.centers), km.max_iter, km.tol > 0.0, km.n_init, km.workers
    println labels.at(0, 0) != labels.at(4, 0), f3(ml.AdjustedRandIndex(labels, truth)), f3(ml.SilhouetteScore(x, labels))
    println km.fit(x), matrix.ShapeString(km.predict(x))
    mut mini := ml.MiniBatchKMeans { k = 2, batch_size = 4, max_iter = 50, seed = 2 }
    println mini.fit(x), mini.ok, matrix.ShapeString(mini.centers), f3(ml.AdjustedRandIndex(mini.fit_predict(x), truth)), matrix.ShapeString(mini.predict(x))

    mut density := ml.DBSCAN { eps = 1.5, min_samples = 3 }
    mut clusters := density.fit_predict(x)
    println density.ok, density.n_clusters, matrix.ShapeString(density.labels), f3(ml.AdjustedRandIndex(clusters, truth)), density.fit(x)

    mut agg := ml.Agglomerative { n_clusters = 2, linkage = ml.LINK_AVERAGE }
    println f3(ml.AdjustedRandIndex(agg.fit_predict(x), truth)), agg.ok, matrix.ShapeString(agg.labels), agg.fit(x)
    println ml.LINK_SINGLE, ml.LINK_COMPLETE

    mut mixture := ml.GaussianMixture { k = 2, seed = 3 }
    println f3(ml.AdjustedRandIndex(mixture.fit_predict(x), truth)), mixture.ok, mixture.n_iter > 0, mixture.log_likelihood != 0.0, mixture.max_iter, mixture.tol > 0.0, mixture.var_floor > 0.0
    println matrix.ShapeString(mixture.means), matrix.ShapeString(mixture.vars), matrix.ShapeString(mixture.weights), mixture.fit(x)
    println matrix.ShapeString(mixture.predict(x)), matrix.ShapeString(mixture.predict_proba(x))

    mut tsne := ml.TSNE { n_components = 2, perplexity = 2.0, n_iter = 250, seed = 4 }
    mut embedded := tsne.fit_transform(x)
    println tsne.ok, matrix.ShapeString(embedded), matrix.ShapeString(tsne.embedding), tsne.kl >= 0.0, tsne.learning_rate, tsne.early_exaggeration
    km.free()
    mini.free()
    density.free()
    agg.free()
    mixture.free()
    tsne.free()
end
Outputtrue true 4.000 2x2 300 true 3 0 true 1.000 0.885 true 8x1 true true 2x2 1.000 8x1 true 2 8x1 1.000 true 1.000 true 8x1 true 0 1 1.000 true true true 100 true true 2x2 2x2 2x1 true 8x1 8x2 true 8x2 8x2 true 0 12

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Cluster.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Cluster.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Cluster.salam

fit_predict

func fit_predict(x &: matrix.Matrix): matrix.Matrix
Fits and returns the cluster label of each row.

The example for this item is shown with the ones above: ml-Cluster.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Cluster.salam

Agglomerative

struct Agglomerative
Agglomerative hierarchical clustering.
NameTypeDescription
n_clustersint = 2The number of clusters.
linkageintThe linkage method, one of the LINK_... constants.
labelsmatrix.MatrixThe cluster label of each row.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Cluster.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Cluster.salam

fit_predict

func fit_predict(x &: matrix.Matrix): matrix.Matrix
Fits and returns the cluster label of each row.

The example for this item is shown with the ones above: ml-Cluster.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Cluster.salam

MiniBatchKMeans

struct MiniBatchKMeans
k-means on small batches, for large data.
NameTypeDescription
kint = 8The number of neighbours or clusters.
batch_sizeint = 64The batch size.
max_iterint = 200The maximum number of iterations.
seedi64 = 42The random seed, for a repeatable result.
centersmatrix.MatrixThe cluster centres.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Cluster.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Cluster.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Cluster.salam

fit_predict

func fit_predict(x &: matrix.Matrix): matrix.Matrix
Fits and returns the cluster label of each row.

The example for this item is shown with the ones above: ml-Cluster.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Cluster.salam

LDA

struct LDA
Linear discriminant analysis.
NameTypeDescription
ridgef64 = 1e-06Covariance regularisation, for stability.
meansmatrix.MatrixThe mean of each class.
covmatrix.MatrixThe shared covariance.
log_priorsmatrix.MatrixThe log prior probability of each class.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

QDA

struct QDA
Quadratic discriminant analysis, with a separate covariance for each class.
NameTypeDescription
ridgef64 = 1e-06Covariance regularisation, for stability.
meansmatrix.MatrixThe mean of each class.
covsVectorThe covariance of each class.
log_priorsmatrix.MatrixThe log prior probability of each class.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

RandomForest

struct RandomForest
A random forest: many trees on random samples.
NameTypeDescription
n_treesint = 100The number of trees.
criterionintThe split criterion, one of the TREE_... constants.
max_depthint = 16The maximum tree depth.
min_samples_leafint = 1The minimum number of samples in a leaf.
max_featuresint = 0How many features each split tries; zero means all of them.
bootstrapbool = trueSample with replacement.
seedi64 = 42The random seed, for a repeatable result.
workersint = 0The number of threads; zero means automatic.
treesVectorThe trees.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.
ml-Trees.salam
import ml
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(matrix.Chop(m, 0.000001), 3)
end

func f3(number: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, number), 3)
end

func ints(v: Vector<int>): str:
    mut text := "["
    repeat v.len() in i:
        if i > 0:
            text += " "
        end
        text += "" + v.get(i)
    end
    ret text + "]"
end

func main:
    mut x := build(8, 2, "1 1 1 2 2 1 2 2 6 6 6 7 7 6 7 7")
    mut y := build(8, 1, "0 0 0 0 1 1 1 1")
    mut test := build(2, 2, "1.5 1.5 6.5 6.5")

    mut tree := ml.DecisionTree { criterion = ml.TREE_GINI, max_depth = 3 }
    println tree.fit(x, y), tree.ok, tree.n_classes, tree.is_regression(), tree.node_count(), tree.apply_row(test, 0) >= 0
    println tree.min_samples_split, tree.min_samples_leaf, tree.max_features, tree.seed, tree.n_classes_hint, tree.feature.len(), tree.threshold.len(), tree.left.len(), tree.right.len(), matrix.ShapeString(tree.value)
    show("tree", tree.predict(test))
    println matrix.ShapeString(tree.predict_proba(test)), f3(tree.score(x, y))
    mut regression := ml.DecisionTree { criterion = ml.TREE_MSE }
    println regression.fit(x, build(8, 1, "1 1 1 1 5 5 5 5")), regression.is_regression(), ml.TREE_ENTROPY

    mut forest := ml.RandomForest { n_trees = 20, max_depth = 4, seed = 3 }
    println forest.fit(x, y), forest.ok, forest.n_classes, forest.trees.len(), forest.is_regression(), forest.criterion, forest.min_samples_leaf, forest.max_features, forest.bootstrap, forest.workers
    show("forest", forest.predict(test))
    println matrix.ShapeString(forest.predict_proba(test)), f3(forest.score(x, y))
    forest.free_trees()

    mut ada := ml.AdaBoost { n_estimators = 10 }
    println ada.fit(x, y), ada.ok, ada.n_classes, ada.trees.len() > 0, ada.alphas.len() > 0, ada.max_depth, ada.learning_rate, ada.seed
    show("adaboost", ada.predict(test))
    println f3(ada.score(x, y))
    ada.free_all()

    mut boost := ml.GradientBoosting { n_estimators = 20, learning_rate = 0.3, max_depth = 2 }
    println boost.fit(x, y), boost.ok, boost.n_classes, boost.trees.len(), boost.min_samples_leaf, boost.regression, matrix.ShapeString(boost.base)
    show("gradient boosting", boost.predict(test))
    println matrix.ShapeString(boost.decision(test)), matrix.ShapeString(boost.predict_proba(test)), f3(boost.score(x, y))
    boost.free_all()

    mut hist := ml.HistGradientBoosting { n_estimators = 20, min_samples_leaf = 1, max_bins = 16 }
    println hist.fit(x, y), hist.ok, hist.n_classes, hist.trees.len() > 0, hist.max_depth, hist.l2, hist.learning_rate, hist.regression, hist.edges.len() > 0, hist.edges_start.len(), hist.base.len()
    show("histogram", hist.predict(test))
    println matrix.ShapeString(hist.decision(test)), matrix.ShapeString(hist.predict_proba(test)), f3(hist.score(x, y))
    mut first := hist.trees.get(0)
    println first.feature.len() > 0, first.threshold.len() > 0, first.left.len() > 0, first.right.len() > 0, first.value.len() > 0
    hist.free_all()

    mut kd := ml.NewKDTree(x, 2)
    mut dists := Vector {} as Vector<f64>
    println kd.ok, kd.leaf_size, kd.node_count() > 0, ints(kd.query(test, 1, 2, dists)), f3(dists.get(0))
    println matrix.ShapeString(kd.x), kd.idx.len(), kd.node_lo.len() > 0, kd.node_hi.len() > 0, kd.node_dim.len() > 0, kd.node_split.len() > 0, kd.node_left.len() > 0, kd.node_right.len() > 0
    mut empty := ml.KDTree {}
    println empty.build(x), empty.node_count() > 0
    kd.free()
    empty.free()
    tree.free()
    regression.free()
end
Outputtrue true 2 false 3 true 2 1 0 42 0 3 3 3 3 3x2 tree [0.000; 1.000] 2x2 1.000 true true 1 true true 2 20 false 0 1 0 true 0 forest [0.000; 1.000] 2x2 1.000 true true 2 true true 1 1 42 adaboost [0.000; 1.000] 1.000 true true 2 40 1 false 1x2 gradient boosting [0.000; 1.000] 2x2 2x2 1.000 true true 2 true 6 1 0.1 false true 3 2 histogram [0.000; 1.000] 2x2 2x2 1.000 true true true true true true 2 true [4 5] 0.500 8x2 8 true true true true true true true true

is_regression

func is_regression(): bool
Whether the model is a regression, not a classifier.

The example for this item is shown with the ones above: ml-Trees.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Trees.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Trees.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Trees.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Trees.salam

free_trees

func free_trees()
Frees the trees.

The example for this item is shown with the ones above: ml-Trees.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Trees.salam

AdaBoost

struct AdaBoost
AdaBoost with shallow trees.
NameTypeDescription
n_estimatorsint = 50The number of estimators.
max_depthint = 1The maximum tree depth.
learning_ratef64 = 1The learning rate.
seedi64 = 42The random seed, for a repeatable result.
treesVectorThe trees.
alphasVectorThe weight of each tree.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Trees.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Trees.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Trees.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Trees.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Trees.salam

free_all

func free_all()
Frees all the trees.

The example for this item is shown with the ones above: ml-Trees.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Trees.salam

GradientBoosting

struct GradientBoosting
Gradient boosting with trees.
NameTypeDescription
n_estimatorsint = 100The number of estimators.
learning_ratef64 = 0.1The learning rate.
max_depthint = 3The maximum tree depth.
min_samples_leafint = 1The minimum number of samples in a leaf.
regressionbool = falseRegression instead of classification.
treesVectorThe trees.
basematrix.MatrixThe initial prediction.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Trees.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Trees.salam

decision

func decision(x &: matrix.Matrix): matrix.Matrix
The decision score before it becomes a class.

The example for this item is shown with the ones above: ml-Trees.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Trees.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Trees.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Trees.salam

free_all

func free_all()
Frees all the trees.

The example for this item is shown with the ones above: ml-Trees.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Trees.salam

LinearReg

struct LinearReg
Linear regression by least squares.
NameTypeDescription
fit_interceptbool = trueWhether to fit an intercept too.
coefmatrix.MatrixThe coefficients.
interceptf64 = 0The intercept.
okbool = falseWhether the fit succeeded.
ml-Linear.salam
import ml
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(matrix.Chop(m, 0.000001), 3)
end

func f3(number: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, number), 3)
end

func main:
    mut x := build(5, 2, "1 0 2 1 3 0 4 1 5 0")
    mut y := build(5, 1, "3 6 7 10 11")
    mut linear := ml.LinearReg {}
    println linear.fit(x, y), linear.ok, linear.fit_intercept, f3(linear.intercept)
    show("coefficients", linear.coef)
    mut pred := linear.predict(x)
    show("prediction", pred)
    println f3(linear.score(x, y)), f3(ml.R2Score(y, pred))
    mut guess := build(5, 1, "3.5 5.5 7 10 12")
    println f3(ml.MSE(y, guess)), f3(ml.MAE(y, guess)), f3(ml.RMSE(y, guess))
    println f3(ml.MedianAbsoluteError(y, guess)), f3(ml.MAPE(y, guess)), f3(ml.ExplainedVariance(y, guess))

    mut ridge := ml.Ridge { alpha = 1.0 }
    println ridge.fit(x, y), ridge.ok, ridge.alpha, ridge.fit_intercept, f3(ridge.intercept)
    show("ridge", ridge.coef)
    println ridge.score(x, y) > 0.9, matrix.ShapeString(ridge.predict(x))
    mut lasso := ml.Lasso { alpha = 0.1 }
    println lasso.fit(x, y), lasso.ok, lasso.alpha, lasso.l1_ratio, lasso.fit_intercept, lasso.max_iter, lasso.tol > 0.0, lasso.n_iter > 0, lasso.intercept > 0.0
    show("lasso", lasso.coef)
    println lasso.score(x, y) > 0.9, matrix.ShapeString(lasso.predict(x))
    mut elastic := ml.NewElasticNet(0.1, 0.5)
    println elastic.fit(x, y), elastic.l1_ratio

    mut hours := build(6, 1, "1 2 3 4 5 6")
    mut passed := build(6, 1, "0 0 0 1 1 1")
    mut logistic := ml.LogisticRegression { alpha = 0.01, max_iter = 2000 }
    println logistic.fit(hours, passed), logistic.ok, logistic.n_classes, logistic.fit_intercept, logistic.n_iter > 0, logistic.tol > 0.0
    println matrix.ShapeString(logistic.coef), matrix.ShapeString(logistic.intercept)
    show("prediction", logistic.predict(hours))
    mut proba := logistic.predict_proba(build(2, 1, "1 6"))
    println proba.at(0, 1) < 0.5, proba.at(1, 1) > 0.5, f3(logistic.score(hours, passed))
    linear.free()
    ridge.free()
    lasso.free()
    elastic.free()
    logistic.free()
end
Outputtrue true true 1.000 coefficients [2.000; 1.000] prediction [3.000; 6.000; 7.000; 10.000; 11.000] 1.000 1.000 0.300 0.400 0.548 0.500 0.068 0.968 true true 1 true 1.727 ridge [1.818; 0.545] true 5x1 true true 0.1 1 true 1000 true true true lasso [1.950; 0.583] true 5x1 true 0.5 true true 2 true true true 1x2 1x2 prediction [0.000; 0.000; 0.000; 1.000; 1.000; 1.000] true true 1.000

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Linear.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Linear.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Linear.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Linear.salam

Ridge

struct Ridge
Ridge regression: linear with an L2 penalty.
NameTypeDescription
alphaf64 = 1The regularisation strength.
fit_interceptbool = trueWhether to fit an intercept too.
coefmatrix.MatrixThe coefficients.
interceptf64 = 0The intercept.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Linear.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Linear.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Linear.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Linear.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Linear.salam

Lasso

struct Lasso
Lasso: linear with an L1 penalty that drives unimportant coefficients to zero; with an l1_ratio below one it is an elastic net.
NameTypeDescription
alphaf64 = 1The regularisation strength.
l1_ratiof64 = 1The L1 share of the penalty; one means lasso.
fit_interceptbool = trueWhether to fit an intercept too.
max_iterint = 1000The maximum number of iterations.
tolf64 = 0.0001The convergence tolerance.
coefmatrix.MatrixThe coefficients.
interceptf64 = 0The intercept.
n_iterint = 0The number of iterations run.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Linear.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Linear.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Linear.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Linear.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Linear.salam

LogisticRegression

struct LogisticRegression
Logistic regression for classification, with any number of classes.
NameTypeDescription
alphaf64 = 0.0001The regularisation strength.
fit_interceptbool = trueWhether to fit an intercept too.
max_iterint = 500The maximum number of iterations.
tolf64 = 1e-06The convergence tolerance.
coefmatrix.MatrixThe coefficients.
interceptmatrix.MatrixThe intercept.
n_classesint = 0The number of classes.
n_iterint = 0The number of iterations run.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Linear.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Linear.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Linear.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Linear.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Linear.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Linear.salam

KDTree

struct KDTree
A k-d tree for nearest neighbour search.
NameTypeDescription
leaf_sizeint = 16The leaf size of the k-d tree.
xmatrix.MatrixThe data.
idxVectorThe row permutation.
node_loVectorThe first row of each node.
node_hiVectorThe end row of each node.
node_dimVectorThe split dimension of each node.
node_splitVectorThe split value of each node.
node_leftVectorThe left child.
node_rightVectorThe right child.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

build

func build(x &: matrix.Matrix): bool
Builds the tree from the data.

The example for this item is shown with the ones above: ml-Classify.salam

node_count

func node_count(): int
The number of nodes.

The example for this item is shown with the ones above: ml-Classify.salam

query

func query(q &: matrix.Matrix, qi: int, k: int, dist_out &: Vector<f64>): Vector<int>
The k nearest neighbours of row qi, with their distances.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

DecisionTree

struct DecisionTree
A decision tree for classification or regression.
NameTypeDescription
criterionintThe split criterion, one of the TREE_... constants.
max_depthint = 16The maximum tree depth.
min_samples_splitint = 2The minimum number of samples needed to split a node.
min_samples_leafint = 1The minimum number of samples in a leaf.
max_featuresint = 0How many features each split tries; zero means all of them.
seedi64 = 42The random seed, for a repeatable result.
n_classes_hintint = 0The number of classes, if it is known in advance.
featureVectorThe split feature of each node.
thresholdVectorThe split threshold of each node.
leftVectorThe left child of each node.
rightVectorThe right child of each node.
valuematrix.MatrixThe value or probabilities of the leaves.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Trees.salam

is_regression

func is_regression(): bool
Whether the model is a regression, not a classifier.

The example for this item is shown with the ones above: ml-Trees.salam

node_count

func node_count(): int
The number of nodes.

The example for this item is shown with the ones above: ml-Trees.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Trees.salam

apply_row

func apply_row(x &: matrix.Matrix, i: int): int
The leaf the row ends up in.

The example for this item is shown with the ones above: ml-Trees.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Trees.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Trees.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Trees.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Trees.salam

KMeans

struct KMeans
k-means clustering.
NameTypeDescription
kint = 8The number of neighbours or clusters.
max_iterint = 300The maximum number of iterations.
tolf64 = 0.0001The convergence tolerance.
n_initint = 3How many runs with different starts; the best one is kept.
seedi64 = 42The random seed, for a repeatable result.
workersint = 0The number of threads; zero means automatic.
centersmatrix.MatrixThe cluster centres.
inertiaf64 = 0The sum of squared distances to the centres.
n_iterint = 0The number of iterations run.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Cluster.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Cluster.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Cluster.salam

fit_predict

func fit_predict(x &: matrix.Matrix): matrix.Matrix
Fits and returns the cluster label of each row.

The example for this item is shown with the ones above: ml-Cluster.salam

transform

func transform(x &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Cluster.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Cluster.salam

DBSCAN

struct DBSCAN
Density clustering; it finds the number of clusters itself and sets the noise apart.
NameTypeDescription
epsf64 = 0.5The neighbourhood radius.
min_samplesint = 5The minimum number of neighbours for a core point.
labelsmatrix.MatrixThe cluster label of each row; -1 means noise.
n_clustersint = 0The number of clusters found.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Cluster.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Cluster.salam

fit_predict

func fit_predict(x &: matrix.Matrix): matrix.Matrix
Fits and returns the cluster label of each row.

The example for this item is shown with the ones above: ml-Cluster.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Cluster.salam

GaussianNB

struct GaussianNB
Gaussian naive Bayes, for continuous features.
NameTypeDescription
var_smoothingf64 = 1e-09Added to the variances for stability.
meansmatrix.MatrixThe mean of each class.
varsmatrix.MatrixThe variances.
log_priorsmatrix.MatrixThe log prior probability of each class.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

MultinomialNB

struct MultinomialNB
Multinomial naive Bayes, for counts such as in text classification.
NameTypeDescription
alphaf64 = 1Laplace smoothing for zero counts.
fit_priorbool = trueWhether to learn the prior probabilities from the data.
log_probmatrix.MatrixThe log probability of each feature in each class.
log_priorsmatrix.MatrixThe log prior probability of each class.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

decision

func decision(x &: matrix.Matrix): matrix.Matrix
The decision score before it becomes a class.

The example for this item is shown with the ones above: ml-Classify.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

TSNE

struct TSNE
t-SNE, for showing high-dimensional data in two or three dimensions.
NameTypeDescription
n_componentsint = 2The number of components.
perplexityf64 = 30The perplexity, roughly the number of effective neighbours.
learning_ratef64 = 0The learning rate.
n_iterint = 500The number of iterations run.
early_exaggerationf64 = 12The early exaggeration.
seedi64 = 42The random seed, for a repeatable result.
embeddingmatrix.MatrixThe output points.
klf64 = 0The final KL divergence.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Cluster.salam

fit_transform

func fit_transform(x &: matrix.Matrix): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Cluster.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Cluster.salam

StandardScaler

struct StandardScaler
Gives every column a mean of zero and a standard deviation of one.
NameTypeDescription
with_meanbool = trueWhether to subtract the mean.
with_stdbool = trueWhether to divide by the standard deviation.
meanmatrix.MatrixThe column means.
stdmatrix.MatrixThe column standard deviations.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Prep.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(x &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

fit_transform

func fit_transform(x &: matrix.Matrix): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Prep.salam

inverse_transform

func inverse_transform(x &: matrix.Matrix): matrix.Matrix
Undoes the transform.

The example for this item is shown with the ones above: ml-Prep.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

MinMaxScaler

struct MinMaxScaler
Maps every column onto the range lo to hi.
NameTypeDescription
lof64 = 0The output minimum.
hif64 = 1The output maximum.
data_minmatrix.MatrixThe column minimums.
data_rangematrix.MatrixThe column ranges.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Prep.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(x &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

fit_transform

func fit_transform(x &: matrix.Matrix): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Prep.salam

inverse_transform

func inverse_transform(x &: matrix.Matrix): matrix.Matrix
Undoes the transform.

The example for this item is shown with the ones above: ml-Prep.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

LabelEncoder

struct LabelEncoder
Turns text labels into numbers.
NameTypeDescription
classesVectorThe labels, in number order.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Prep.salam

fit

func fit(labels &: Vector<str>): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Prep.salam

index_of

func index_of(label: str): int
The number of a label, or -1 if it is not there.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(labels &: Vector<str>): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

fit_transform

func fit_transform(labels &: Vector<str>): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Prep.salam

inverse_transform

func inverse_transform(y &: matrix.Matrix): Vector<str>
Undoes the transform.

The example for this item is shown with the ones above: ml-Prep.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

OneHotEncoder

struct OneHotEncoder
Turns a class number into a one-hot vector.
NameTypeDescription
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Prep.salam

fit

func fit(y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(y &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

fit_transform

func fit_transform(y &: matrix.Matrix): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Prep.salam

inverse_transform

func inverse_transform(onehot &: matrix.Matrix): matrix.Matrix
Undoes the transform.

The example for this item is shown with the ones above: ml-Prep.salam

SimpleImputer

struct SimpleImputer
Fills in missing values, meaning NaN.
NameTypeDescription
strategyintThe strategy, one of the IMPUTE_... constants.
fillf64 = 0The value for the constant strategy.
statsmatrix.MatrixThe replacement value for each column.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Prep.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(x &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

fit_transform

func fit_transform(x &: matrix.Matrix): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Prep.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

PolynomialFeatures

struct PolynomialFeatures
Polynomial features and the cross products.
NameTypeDescription
degreeint = 2The degree.
include_biasbool = falseWhether to add a column of ones too.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(x &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

LinearSVM

struct LinearSVM
A linear support vector machine.
NameTypeDescription
cf64 = 1The error penalty; larger means a harder margin.
max_iterint = 1000The maximum number of iterations.
tolf64 = 0.0001The convergence tolerance.
seedi64 = 42The random seed, for a repeatable result.
coefmatrix.MatrixThe coefficients.
interceptmatrix.MatrixThe intercept.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

decision

func decision(x &: matrix.Matrix): matrix.Matrix
The decision score before it becomes a class.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

SVC

struct SVC
A kernel support vector machine.
NameTypeDescription
cf64 = 1The error penalty; larger means a harder margin.
kernelintThe kernel, one of the KERNEL_... constants.
gammaf64 = 0The RBF kernel parameter; zero means automatic.
degreeint = 3The degree of the polynomial kernel.
coef0f64 = 1The constant term of the polynomial kernel.
max_passesint = 20The maximum number of SMO passes with no change.
tolf64 = 0.001The convergence tolerance.
seedi64 = 42The random seed, for a repeatable result.
supportmatrix.MatrixThe support vectors.
dualmatrix.MatrixThe dual coefficients.
biasmatrix.MatrixThe bias.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Classify.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Classify.salam

decision

func decision(x &: matrix.Matrix): matrix.Matrix
The decision score before it becomes a class.

The example for this item is shown with the ones above: ml-Classify.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Classify.salam

n_support

func n_support(): int
The number of support vectors.

The example for this item is shown with the ones above: ml-Classify.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Classify.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Classify.salam

HistTree

struct HistTree
One histogram boosting tree.
NameTypeDescription
featureVectorThe split feature of each node.
thresholdVectorThe split threshold of each node.
leftVectorThe left child of each node.
rightVectorThe right child of each node.
valueVectorThe value or probabilities of the leaves.

The example for this item is shown with the ones above: ml-Trees.salam

apply_row

func apply_row(x &: matrix.Matrix, i: int): f64
The leaf the row ends up in.

The example for this item is shown with the ones above: ml-Trees.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Trees.salam

HistGradientBoosting

struct HistGradientBoosting
Histogram gradient boosting, fast on large data, like LightGBM.
NameTypeDescription
n_estimatorsint = 100The number of estimators.
learning_ratef64 = 0.1The learning rate.
max_depthint = 6The maximum tree depth.
max_binsint = 255The maximum number of bins per feature.
min_samples_leafint = 20The minimum number of samples in a leaf.
l2f64 = 1L2 regularisation of the leaves.
regressionbool = falseRegression instead of classification.
edgesVectorThe bin edges.
edges_startVectorWhere the edges of each feature start.
baseVectorThe initial prediction.
treesVectorThe trees.
n_classesint = 0The number of classes.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Trees.salam

fit

func fit(x &: matrix.Matrix, y &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Trees.salam

decision

func decision(x &: matrix.Matrix): matrix.Matrix
The decision score before it becomes a class.

The example for this item is shown with the ones above: ml-Trees.salam

predict_proba

func predict_proba(x &: matrix.Matrix): matrix.Matrix
The probability of each class for each row.

The example for this item is shown with the ones above: ml-Trees.salam

predict

func predict(x &: matrix.Matrix): matrix.Matrix
The prediction for each row.

The example for this item is shown with the ones above: ml-Trees.salam

score

func score(x &: matrix.Matrix, y &: matrix.Matrix): f64
The score on the data: accuracy for classification and R² for regression.

The example for this item is shown with the ones above: ml-Trees.salam

free_all

func free_all()
Frees all the trees.

The example for this item is shown with the ones above: ml-Trees.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Trees.salam

PCA

struct PCA
Principal component analysis, for reducing dimensions.
NameTypeDescription
n_componentsint = 0The number of components.
componentsmatrix.MatrixThe components.
meanmatrix.MatrixThe column means.
explained_variancematrix.MatrixThe variance of each component.
explained_ratiomatrix.MatrixThe share of the variance each component explains.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Prep.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(x &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

fit_transform

func fit_transform(x &: matrix.Matrix): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Prep.salam

inverse_transform

func inverse_transform(z &: matrix.Matrix): matrix.Matrix
Undoes the transform.

The example for this item is shown with the ones above: ml-Prep.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

TruncatedSVD

struct TruncatedSVD
Truncated singular value decomposition: like PCA without subtracting the mean, for sparse data.
NameTypeDescription
n_componentsint = 2The number of components.
componentsmatrix.MatrixThe components.
singular_valuesmatrix.MatrixThe singular values.
explained_variancematrix.MatrixThe variance of each component.
explained_ratiomatrix.MatrixThe share of the variance each component explains.
okbool = falseWhether the fit succeeded.

The example for this item is shown with the ones above: ml-Prep.salam

fit

func fit(x &: matrix.Matrix): bool
Fits the model on the training data.

The example for this item is shown with the ones above: ml-Prep.salam

transform

func transform(x &: matrix.Matrix): matrix.Matrix
Transforms the data.

The example for this item is shown with the ones above: ml-Prep.salam

fit_transform

func fit_transform(x &: matrix.Matrix): matrix.Matrix
Fits, then transforms the same data.

The example for this item is shown with the ones above: ml-Prep.salam

inverse_transform

func inverse_transform(z &: matrix.Matrix): matrix.Matrix
Undoes the transform.

The example for this item is shown with the ones above: ml-Prep.salam

free

func free()
Frees the memory.

The example for this item is shown with the ones above: ml-Prep.salam

Constants and variables

TREE_GINI

const TREE_GINI: i32 = 0
Split criterion: Gini.

The example for this item is shown with the ones above: ml-Trees.salam

TREE_ENTROPY

const TREE_ENTROPY: i32 = 1
Split criterion: entropy.

The example for this item is shown with the ones above: ml-Trees.salam

TREE_MSE

const TREE_MSE: i32 = 2
Split criterion: mean squared error, for regression.

The example for this item is shown with the ones above: ml-Trees.salam

IMPUTE_MEAN

const IMPUTE_MEAN: i32 = 0
Impute with the mean.

The example for this item is shown with the ones above: ml-Prep.salam

IMPUTE_MEDIAN

const IMPUTE_MEDIAN: i32 = 1
Impute with the median.

The example for this item is shown with the ones above: ml-Prep.salam

IMPUTE_CONSTANT

const IMPUTE_CONSTANT: i32 = 2
Impute with a constant value.

The example for this item is shown with the ones above: ml-Prep.salam

ML_TOL

const ML_TOL: f64 = 1e-06
The default tolerance.

The example for this item is shown with the ones above: ml-Prep.salam

const LINK_SINGLE: i32 = 0
Linkage: the two closest members.

The example for this item is shown with the ones above: ml-Cluster.salam

const LINK_COMPLETE: i32 = 1
Linkage: the two farthest members.

The example for this item is shown with the ones above: ml-Cluster.salam

const LINK_AVERAGE: i32 = 2
Linkage: the mean of the distances.

The example for this item is shown with the ones above: ml-Cluster.salam

KNN_BRUTE

const KNN_BRUTE: i32 = 0
Neighbour search: compare against every row.

The example for this item is shown with the ones above: ml-Classify.salam

KNN_KDTREE

const KNN_KDTREE: i32 = 1
Neighbour search: a k-d tree, fast in low dimensions.

The example for this item is shown with the ones above: ml-Classify.salam

KERNEL_LINEAR

const KERNEL_LINEAR: i32 = 0
The linear kernel.

The example for this item is shown with the ones above: ml-Classify.salam

KERNEL_RBF

const KERNEL_RBF: i32 = 1
The Gaussian radial basis function (RBF) kernel.

The example for this item is shown with the ones above: ml-Classify.salam

KERNEL_POLY

const KERNEL_POLY: i32 = 2
The polynomial kernel.

The example for this item is shown with the ones above: ml-Classify.salam

Functions

LabelsToInts

func LabelsToInts(y &: matrix.Matrix): Vector<int>
Float labels as integers.

The example for this item is shown with the ones above: ml-Prep.salam

CountClasses

func CountClasses(y &: matrix.Matrix): int
The number of classes.

The example for this item is shown with the ones above: ml-Prep.salam

TrainTestSplit

func TrainTestSplit(x &: matrix.Matrix, y &: matrix.Matrix, test_frac: f64, seed: i64): Split
Splits the data into training and test sets, with a seed.

The example for this item is shown with the ones above: ml-Prep.salam

KFold

func KFold(n: int, k: int, shuffle: bool, seed: i64): Folds
k folds, with optional shuffling.

The example for this item is shown with the ones above: ml-Prep.salam

StratifiedKFold

func StratifiedKFold(y &: matrix.Matrix, k: int, seed: i64): Folds
k folds with the same class proportions in every fold.

The example for this item is shown with the ones above: ml-Prep.salam

CrossValScore

func CrossValScore(fitpred: func(matrix.Matrix, matrix.Matrix, matrix.Matrix): matrix.Matrix, metric: func(matrix.Matrix, matrix.Matrix): f64, x &: matrix.Matrix, y &: matrix.Matrix, folds &: Folds): Vector<f64>
The score of each fold, from a fit-and-predict function and a scoring function.

The example for this item is shown with the ones above: ml-Select.salam

MeanScore

func MeanScore(scores &: Vector<f64>): f64
The mean of the scores.

The example for this item is shown with the ones above: ml-Select.salam

CrossValScoreParallel

func CrossValScoreParallel(fitpred: func(matrix.Matrix, matrix.Matrix, matrix.Matrix): matrix.Matrix, metric: func(matrix.Matrix, matrix.Matrix): f64, x &: matrix.Matrix, y &: matrix.Matrix, folds &: Folds, workers: int): Vector<f64>
The same, on several threads.

The example for this item is shown with the ones above: ml-Select.salam

GridSearch

func GridSearch(candidates &: Vector<Candidate>, metric: func(matrix.Matrix, matrix.Matrix): f64, x &: matrix.Matrix, y &: matrix.Matrix, folds &: Folds, workers: int): GridResult
Finds the best candidate by cross-validation.

The example for this item is shown with the ones above: ml-Select.salam

SilhouetteScore

func SilhouetteScore(x &: matrix.Matrix, labels &: matrix.Matrix): f64
The silhouette score; close to one means compact, well separated clusters.

The example for this item is shown with the ones above: ml-Cluster.salam

AdjustedRandIndex

func AdjustedRandIndex(a &: matrix.Matrix, b &: matrix.Matrix): f64
The adjusted Rand index; one means the labels are exactly the true clustering.

The example for this item is shown with the ones above: ml-Cluster.salam

NewElasticNet

func NewElasticNet(alpha: f64, l1_ratio: f64): Lasso
An elastic net: a mix of L1 and L2.

The example for this item is shown with the ones above: ml-Linear.salam

NewKDTree

func NewKDTree(x &: matrix.Matrix, leaf_size: int): KDTree
A k-d tree over the data, with a leaf size.

The example for this item is shown with the ones above: ml-Trees.salam

Accuracy

func Accuracy(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Accuracy: the share of correct predictions.

The example for this item is shown with the ones above: ml-Linear.salam

ConfusionMatrix

func ConfusionMatrix(y &: matrix.Matrix, pred &: matrix.Matrix, n_classes: int): matrix.Matrix
The confusion matrix; rows are the true class and columns the prediction.

The example for this item is shown with the ones above: ml-Classify.salam

Precision

func Precision(y &: matrix.Matrix, pred &: matrix.Matrix, positive: f64): f64
Precision: how many of the positive predictions were right.

The example for this item is shown with the ones above: ml-Classify.salam

Recall

func Recall(y &: matrix.Matrix, pred &: matrix.Matrix, positive: f64): f64
Recall: how many of the real positives were found.

The example for this item is shown with the ones above: ml-Classify.salam

F1

func F1(y &: matrix.Matrix, pred &: matrix.Matrix, positive: f64): f64
The harmonic mean of precision and recall.

The example for this item is shown with the ones above: ml-Classify.salam

MacroF1

func MacroF1(y &: matrix.Matrix, pred &: matrix.Matrix, n_classes: int): f64
The mean F1 over all classes.

The example for this item is shown with the ones above: ml-Classify.salam

RocAuc

func RocAuc(y &: matrix.Matrix, score &: matrix.Matrix): f64
The area under the ROC curve.

The example for this item is shown with the ones above: ml-Classify.salam

LogLoss

func LogLoss(y &: matrix.Matrix, proba &: matrix.Matrix): f64
The log loss of the probabilities.

The example for this item is shown with the ones above: ml-Classify.salam

MSE

func MSE(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Mean squared error.

The example for this item is shown with the ones above: ml-Linear.salam

MAE

func MAE(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Mean absolute error.

The example for this item is shown with the ones above: ml-Linear.salam

RMSE

func RMSE(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Root mean squared error.

The example for this item is shown with the ones above: ml-Linear.salam

R2Score

func R2Score(y &: matrix.Matrix, pred &: matrix.Matrix): f64
The coefficient of determination, R².

The example for this item is shown with the ones above: ml-Linear.salam

RocCurve

func RocCurve(y &: matrix.Matrix, score &: matrix.Matrix): matrix.Matrix
The points of the ROC curve: false positive rate, true positive rate and threshold.

The example for this item is shown with the ones above: ml-Classify.salam

PrecisionRecallCurve

func PrecisionRecallCurve(y &: matrix.Matrix, score &: matrix.Matrix): matrix.Matrix
The points of the precision and recall curve.

The example for this item is shown with the ones above: ml-Classify.salam

AveragePrecision

func AveragePrecision(y &: matrix.Matrix, score &: matrix.Matrix): f64
Average precision.

The example for this item is shown with the ones above: ml-Classify.salam

BalancedAccuracy

func BalancedAccuracy(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Balanced accuracy, for imbalanced classes.

The example for this item is shown with the ones above: ml-Classify.salam

MatthewsCorrCoef

func MatthewsCorrCoef(y &: matrix.Matrix, pred &: matrix.Matrix): f64
The Matthews correlation coefficient.

The example for this item is shown with the ones above: ml-Classify.salam

CohenKappa

func CohenKappa(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Cohen's kappa.

The example for this item is shown with the ones above: ml-Classify.salam

ExplainedVariance

func ExplainedVariance(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Explained variance.

The example for this item is shown with the ones above: ml-Linear.salam

MedianAbsoluteError

func MedianAbsoluteError(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Median absolute error.

The example for this item is shown with the ones above: ml-Linear.salam

MAPE

func MAPE(y &: matrix.Matrix, pred &: matrix.Matrix): f64
Mean absolute percentage error.

The example for this item is shown with the ones above: ml-Linear.salam