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| Name | Type | Description |
|---|---|---|
x_train | matrix.Matrix | The training features. |
x_test | matrix.Matrix | The test features. |
y_train | matrix.Matrix | The training labels. |
y_test | matrix.Matrix | The test labels. |
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()
endfree
func free()The example for this item is shown with the ones above: ml-Prep.salam
Folds
struct Folds| Name | Type | Description |
|---|---|---|
train | Vector | The training row numbers of each fold. |
test | Vector | The test row numbers of each fold. |
The example for this item is shown with the ones above: ml-Prep.salam
count
func count(): intThe example for this item is shown with the ones above: ml-Prep.salam
free
func free()The example for this item is shown with the ones above: ml-Prep.salam
GridResult
struct GridResult| Name | Type | Description |
|---|---|---|
scores | Vector | The mean score of each candidate. |
best | int = -1 | The number of the best one. |
best_score | f64 = 0 | The best score. |
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
endfree
func free()The example for this item is shown with the ones above: ml-Select.salam
Candidate
struct Candidate| Name | Type | Description |
|---|---|---|
name | str = "" | The name. |
fit | func(matrix.Matrix, matrix.Matrix, matrix.Matrix): matrix.Matrix | A 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| Name | Type | Description |
|---|---|---|
k | int = 5 | The number of neighbours or clusters. |
weighted | bool = false | Weights the neighbours by inverse distance. |
algorithm | int | The search method, one of the KNN_... constants. |
leaf_size | int = 16 | The leaf size of the k-d tree. |
workers | int = 0 | The number of threads; zero means automatic. |
xtrain | matrix.Matrix | The training data. |
ytrain | matrix.Matrix | The training labels. |
tree | KDTree | The k-d tree. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether the fit succeeded. |
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()
endfit
func fit(x &: matrix.Matrix, y &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Classify.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Select.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
KNNRegressor
struct KNNRegressor| Name | Type | Description |
|---|---|---|
k | int = 5 | The number of neighbours or clusters. |
weighted | bool = false | Weights the neighbours by inverse distance. |
algorithm | int | The search method, one of the KNN_... constants. |
leaf_size | int = 16 | The leaf size of the k-d tree. |
workers | int = 0 | The number of threads; zero means automatic. |
xtrain | matrix.Matrix | The training data. |
ytrain | matrix.Matrix | The training labels. |
tree | KDTree | The k-d tree. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
GaussianMixture
struct GaussianMixture| Name | Type | Description |
|---|---|---|
k | int = 2 | The number of neighbours or clusters. |
max_iter | int = 100 | The maximum number of iterations. |
tol | f64 = 0.0001 | The convergence tolerance. |
var_floor | f64 = 1e-06 | The minimum variance. |
seed | i64 = 42 | The random seed, for a repeatable result. |
means | matrix.Matrix | The mean of each class. |
vars | matrix.Matrix | The variances. |
weights | matrix.Matrix | The weight of each component. |
log_likelihood | f64 = 0 | The log likelihood. |
n_iter | int = 0 | The number of iterations run. |
ok | bool = false | Whether the fit succeeded. |
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()
endfit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Cluster.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
fit_predict
func fit_predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
free
func free()The example for this item is shown with the ones above: ml-Cluster.salam
Agglomerative
struct Agglomerative| Name | Type | Description |
|---|---|---|
n_clusters | int = 2 | The number of clusters. |
linkage | int | The linkage method, one of the LINK_... constants. |
labels | matrix.Matrix | The cluster label of each row. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Cluster.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Cluster.salam
fit_predict
func fit_predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
free
func free()The example for this item is shown with the ones above: ml-Cluster.salam
MiniBatchKMeans
struct MiniBatchKMeans| Name | Type | Description |
|---|---|---|
k | int = 8 | The number of neighbours or clusters. |
batch_size | int = 64 | The batch size. |
max_iter | int = 200 | The maximum number of iterations. |
seed | i64 = 42 | The random seed, for a repeatable result. |
centers | matrix.Matrix | The cluster centres. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Cluster.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Cluster.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
fit_predict
func fit_predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
free
func free()The example for this item is shown with the ones above: ml-Cluster.salam
LDA
struct LDA| Name | Type | Description |
|---|---|---|
ridge | f64 = 1e-06 | Covariance regularisation, for stability. |
means | matrix.Matrix | The mean of each class. |
cov | matrix.Matrix | The shared covariance. |
log_priors | matrix.Matrix | The log prior probability of each class. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Classify.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
QDA
struct QDA| Name | Type | Description |
|---|---|---|
ridge | f64 = 1e-06 | Covariance regularisation, for stability. |
means | matrix.Matrix | The mean of each class. |
covs | Vector | The covariance of each class. |
log_priors | matrix.Matrix | The log prior probability of each class. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Classify.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
RandomForest
struct RandomForest| Name | Type | Description |
|---|---|---|
n_trees | int = 100 | The number of trees. |
criterion | int | The split criterion, one of the TREE_... constants. |
max_depth | int = 16 | The maximum tree depth. |
min_samples_leaf | int = 1 | The minimum number of samples in a leaf. |
max_features | int = 0 | How many features each split tries; zero means all of them. |
bootstrap | bool = true | Sample with replacement. |
seed | i64 = 42 | The random seed, for a repeatable result. |
workers | int = 0 | The number of threads; zero means automatic. |
trees | Vector | The trees. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether the fit succeeded. |
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()
endis_regression
func is_regression(): boolThe example for this item is shown with the ones above: ml-Trees.salam
fit
func fit(x &: matrix.Matrix, y &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Trees.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Trees.salam
free_trees
func free_trees()The example for this item is shown with the ones above: ml-Trees.salam
free
func free()The example for this item is shown with the ones above: ml-Trees.salam
AdaBoost
struct AdaBoost| Name | Type | Description |
|---|---|---|
n_estimators | int = 50 | The number of estimators. |
max_depth | int = 1 | The maximum tree depth. |
learning_rate | f64 = 1 | The learning rate. |
seed | i64 = 42 | The random seed, for a repeatable result. |
trees | Vector | The trees. |
alphas | Vector | The weight of each tree. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Trees.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Trees.salam
free_all
func free_all()The example for this item is shown with the ones above: ml-Trees.salam
free
func free()The example for this item is shown with the ones above: ml-Trees.salam
GradientBoosting
struct GradientBoosting| Name | Type | Description |
|---|---|---|
n_estimators | int = 100 | The number of estimators. |
learning_rate | f64 = 0.1 | The learning rate. |
max_depth | int = 3 | The maximum tree depth. |
min_samples_leaf | int = 1 | The minimum number of samples in a leaf. |
regression | bool = false | Regression instead of classification. |
trees | Vector | The trees. |
base | matrix.Matrix | The initial prediction. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Trees.salam
decision
func decision(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Trees.salam
free_all
func free_all()The example for this item is shown with the ones above: ml-Trees.salam
free
func free()The example for this item is shown with the ones above: ml-Trees.salam
LinearReg
struct LinearReg| Name | Type | Description |
|---|---|---|
fit_intercept | bool = true | Whether to fit an intercept too. |
coef | matrix.Matrix | The coefficients. |
intercept | f64 = 0 | The intercept. |
ok | bool = false | Whether the fit succeeded. |
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()
endfit
func fit(x &: matrix.Matrix, y &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Linear.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Linear.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
free
func free()The example for this item is shown with the ones above: ml-Linear.salam
Ridge
struct Ridge| Name | Type | Description |
|---|---|---|
alpha | f64 = 1 | The regularisation strength. |
fit_intercept | bool = true | Whether to fit an intercept too. |
coef | matrix.Matrix | The coefficients. |
intercept | f64 = 0 | The intercept. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Linear.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Linear.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
free
func free()The example for this item is shown with the ones above: ml-Linear.salam
Lasso
struct Lasso| Name | Type | Description |
|---|---|---|
alpha | f64 = 1 | The regularisation strength. |
l1_ratio | f64 = 1 | The L1 share of the penalty; one means lasso. |
fit_intercept | bool = true | Whether to fit an intercept too. |
max_iter | int = 1000 | The maximum number of iterations. |
tol | f64 = 0.0001 | The convergence tolerance. |
coef | matrix.Matrix | The coefficients. |
intercept | f64 = 0 | The intercept. |
n_iter | int = 0 | The number of iterations run. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Linear.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Linear.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
free
func free()The example for this item is shown with the ones above: ml-Linear.salam
LogisticRegression
struct LogisticRegression| Name | Type | Description |
|---|---|---|
alpha | f64 = 0.0001 | The regularisation strength. |
fit_intercept | bool = true | Whether to fit an intercept too. |
max_iter | int = 500 | The maximum number of iterations. |
tol | f64 = 1e-06 | The convergence tolerance. |
coef | matrix.Matrix | The coefficients. |
intercept | matrix.Matrix | The intercept. |
n_classes | int = 0 | The number of classes. |
n_iter | int = 0 | The number of iterations run. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Linear.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Linear.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Linear.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
free
func free()The example for this item is shown with the ones above: ml-Linear.salam
KDTree
struct KDTree| Name | Type | Description |
|---|---|---|
leaf_size | int = 16 | The leaf size of the k-d tree. |
x | matrix.Matrix | The data. |
idx | Vector | The row permutation. |
node_lo | Vector | The first row of each node. |
node_hi | Vector | The end row of each node. |
node_dim | Vector | The split dimension of each node. |
node_split | Vector | The split value of each node. |
node_left | Vector | The left child. |
node_right | Vector | The right child. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Classify.salam
build
func build(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Classify.salam
node_count
func node_count(): intThe 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 example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
DecisionTree
struct DecisionTree| Name | Type | Description |
|---|---|---|
criterion | int | The split criterion, one of the TREE_... constants. |
max_depth | int = 16 | The maximum tree depth. |
min_samples_split | int = 2 | The minimum number of samples needed to split a node. |
min_samples_leaf | int = 1 | The minimum number of samples in a leaf. |
max_features | int = 0 | How many features each split tries; zero means all of them. |
seed | i64 = 42 | The random seed, for a repeatable result. |
n_classes_hint | int = 0 | The number of classes, if it is known in advance. |
feature | Vector | The split feature of each node. |
threshold | Vector | The split threshold of each node. |
left | Vector | The left child of each node. |
right | Vector | The right child of each node. |
value | matrix.Matrix | The value or probabilities of the leaves. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Trees.salam
is_regression
func is_regression(): boolThe example for this item is shown with the ones above: ml-Trees.salam
node_count
func node_count(): intThe example for this item is shown with the ones above: ml-Trees.salam
fit
func fit(x &: matrix.Matrix, y &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Trees.salam
apply_row
func apply_row(x &: matrix.Matrix, i: int): intThe example for this item is shown with the ones above: ml-Trees.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Trees.salam
free
func free()The example for this item is shown with the ones above: ml-Trees.salam
KMeans
struct KMeans| Name | Type | Description |
|---|---|---|
k | int = 8 | The number of neighbours or clusters. |
max_iter | int = 300 | The maximum number of iterations. |
tol | f64 = 0.0001 | The convergence tolerance. |
n_init | int = 3 | How many runs with different starts; the best one is kept. |
seed | i64 = 42 | The random seed, for a repeatable result. |
workers | int = 0 | The number of threads; zero means automatic. |
centers | matrix.Matrix | The cluster centres. |
inertia | f64 = 0 | The sum of squared distances to the centres. |
n_iter | int = 0 | The number of iterations run. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Cluster.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Cluster.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
fit_predict
func fit_predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
transform
func transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
free
func free()The example for this item is shown with the ones above: ml-Cluster.salam
DBSCAN
struct DBSCAN| Name | Type | Description |
|---|---|---|
eps | f64 = 0.5 | The neighbourhood radius. |
min_samples | int = 5 | The minimum number of neighbours for a core point. |
labels | matrix.Matrix | The cluster label of each row; -1 means noise. |
n_clusters | int = 0 | The number of clusters found. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Cluster.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Cluster.salam
fit_predict
func fit_predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
free
func free()The example for this item is shown with the ones above: ml-Cluster.salam
GaussianNB
struct GaussianNB| Name | Type | Description |
|---|---|---|
var_smoothing | f64 = 1e-09 | Added to the variances for stability. |
means | matrix.Matrix | The mean of each class. |
vars | matrix.Matrix | The variances. |
log_priors | matrix.Matrix | The log prior probability of each class. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Classify.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
MultinomialNB
struct MultinomialNB| Name | Type | Description |
|---|---|---|
alpha | f64 = 1 | Laplace smoothing for zero counts. |
fit_prior | bool = true | Whether to learn the prior probabilities from the data. |
log_prob | matrix.Matrix | The log probability of each feature in each class. |
log_priors | matrix.Matrix | The log prior probability of each class. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Classify.salam
decision
func decision(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
TSNE
struct TSNE| Name | Type | Description |
|---|---|---|
n_components | int = 2 | The number of components. |
perplexity | f64 = 30 | The perplexity, roughly the number of effective neighbours. |
learning_rate | f64 = 0 | The learning rate. |
n_iter | int = 500 | The number of iterations run. |
early_exaggeration | f64 = 12 | The early exaggeration. |
seed | i64 = 42 | The random seed, for a repeatable result. |
embedding | matrix.Matrix | The output points. |
kl | f64 = 0 | The final KL divergence. |
ok | bool = false | Whether 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.MatrixThe example for this item is shown with the ones above: ml-Cluster.salam
free
func free()The example for this item is shown with the ones above: ml-Cluster.salam
StandardScaler
struct StandardScaler| Name | Type | Description |
|---|---|---|
with_mean | bool = true | Whether to subtract the mean. |
with_std | bool = true | Whether to divide by the standard deviation. |
mean | matrix.Matrix | The column means. |
std | matrix.Matrix | The column standard deviations. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Prep.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Prep.salam
transform
func transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
fit_transform
func fit_transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
inverse_transform
func inverse_transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
free
func free()The example for this item is shown with the ones above: ml-Prep.salam
MinMaxScaler
struct MinMaxScaler| Name | Type | Description |
|---|---|---|
lo | f64 = 0 | The output minimum. |
hi | f64 = 1 | The output maximum. |
data_min | matrix.Matrix | The column minimums. |
data_range | matrix.Matrix | The column ranges. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Prep.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Prep.salam
transform
func transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
fit_transform
func fit_transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
inverse_transform
func inverse_transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
free
func free()The example for this item is shown with the ones above: ml-Prep.salam
LabelEncoder
struct LabelEncoder| Name | Type | Description |
|---|---|---|
classes | Vector | The labels, in number order. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Prep.salam
fit
func fit(labels &: Vector<str>): boolThe example for this item is shown with the ones above: ml-Prep.salam
index_of
func index_of(label: str): intThe example for this item is shown with the ones above: ml-Prep.salam
transform
func transform(labels &: Vector<str>): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
fit_transform
func fit_transform(labels &: Vector<str>): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
inverse_transform
func inverse_transform(y &: matrix.Matrix): Vector<str>The example for this item is shown with the ones above: ml-Prep.salam
free
func free()The example for this item is shown with the ones above: ml-Prep.salam
OneHotEncoder
struct OneHotEncoder| Name | Type | Description |
|---|---|---|
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Prep.salam
fit
func fit(y &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Prep.salam
transform
func transform(y &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
fit_transform
func fit_transform(y &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
inverse_transform
func inverse_transform(onehot &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
SimpleImputer
struct SimpleImputer| Name | Type | Description |
|---|---|---|
strategy | int | The strategy, one of the IMPUTE_... constants. |
fill | f64 = 0 | The value for the constant strategy. |
stats | matrix.Matrix | The replacement value for each column. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Prep.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Prep.salam
transform
func transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
fit_transform
func fit_transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
free
func free()The example for this item is shown with the ones above: ml-Prep.salam
PolynomialFeatures
struct PolynomialFeatures| Name | Type | Description |
|---|---|---|
degree | int = 2 | The degree. |
include_bias | bool = false | Whether 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.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
LinearSVM
struct LinearSVM| Name | Type | Description |
|---|---|---|
c | f64 = 1 | The error penalty; larger means a harder margin. |
max_iter | int = 1000 | The maximum number of iterations. |
tol | f64 = 0.0001 | The convergence tolerance. |
seed | i64 = 42 | The random seed, for a repeatable result. |
coef | matrix.Matrix | The coefficients. |
intercept | matrix.Matrix | The intercept. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Classify.salam
decision
func decision(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
SVC
struct SVC| Name | Type | Description |
|---|---|---|
c | f64 = 1 | The error penalty; larger means a harder margin. |
kernel | int | The kernel, one of the KERNEL_... constants. |
gamma | f64 = 0 | The RBF kernel parameter; zero means automatic. |
degree | int = 3 | The degree of the polynomial kernel. |
coef0 | f64 = 1 | The constant term of the polynomial kernel. |
max_passes | int = 20 | The maximum number of SMO passes with no change. |
tol | f64 = 0.001 | The convergence tolerance. |
seed | i64 = 42 | The random seed, for a repeatable result. |
support | matrix.Matrix | The support vectors. |
dual | matrix.Matrix | The dual coefficients. |
bias | matrix.Matrix | The bias. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Classify.salam
decision
func decision(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
n_support
func n_support(): intThe example for this item is shown with the ones above: ml-Classify.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
free
func free()The example for this item is shown with the ones above: ml-Classify.salam
HistTree
struct HistTree| Name | Type | Description |
|---|---|---|
feature | Vector | The split feature of each node. |
threshold | Vector | The split threshold of each node. |
left | Vector | The left child of each node. |
right | Vector | The right child of each node. |
value | Vector | The 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): f64The example for this item is shown with the ones above: ml-Trees.salam
free
func free()The example for this item is shown with the ones above: ml-Trees.salam
HistGradientBoosting
struct HistGradientBoosting| Name | Type | Description |
|---|---|---|
n_estimators | int = 100 | The number of estimators. |
learning_rate | f64 = 0.1 | The learning rate. |
max_depth | int = 6 | The maximum tree depth. |
max_bins | int = 255 | The maximum number of bins per feature. |
min_samples_leaf | int = 20 | The minimum number of samples in a leaf. |
l2 | f64 = 1 | L2 regularisation of the leaves. |
regression | bool = false | Regression instead of classification. |
edges | Vector | The bin edges. |
edges_start | Vector | Where the edges of each feature start. |
base | Vector | The initial prediction. |
trees | Vector | The trees. |
n_classes | int = 0 | The number of classes. |
ok | bool = false | Whether 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): boolThe example for this item is shown with the ones above: ml-Trees.salam
decision
func decision(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
predict_proba
func predict_proba(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
predict
func predict(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Trees.salam
score
func score(x &: matrix.Matrix, y &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Trees.salam
free_all
func free_all()The example for this item is shown with the ones above: ml-Trees.salam
free
func free()The example for this item is shown with the ones above: ml-Trees.salam
PCA
struct PCA| Name | Type | Description |
|---|---|---|
n_components | int = 0 | The number of components. |
components | matrix.Matrix | The components. |
mean | matrix.Matrix | The column means. |
explained_variance | matrix.Matrix | The variance of each component. |
explained_ratio | matrix.Matrix | The share of the variance each component explains. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Prep.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Prep.salam
transform
func transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
fit_transform
func fit_transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
inverse_transform
func inverse_transform(z &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
free
func free()The example for this item is shown with the ones above: ml-Prep.salam
TruncatedSVD
struct TruncatedSVD| Name | Type | Description |
|---|---|---|
n_components | int = 2 | The number of components. |
components | matrix.Matrix | The components. |
singular_values | matrix.Matrix | The singular values. |
explained_variance | matrix.Matrix | The variance of each component. |
explained_ratio | matrix.Matrix | The share of the variance each component explains. |
ok | bool = false | Whether the fit succeeded. |
The example for this item is shown with the ones above: ml-Prep.salam
fit
func fit(x &: matrix.Matrix): boolThe example for this item is shown with the ones above: ml-Prep.salam
transform
func transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
fit_transform
func fit_transform(x &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
inverse_transform
func inverse_transform(z &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Prep.salam
free
func free()The example for this item is shown with the ones above: ml-Prep.salam
Constants and variables
TREE_GINI
const TREE_GINI: i32 = 0The example for this item is shown with the ones above: ml-Trees.salam
TREE_ENTROPY
const TREE_ENTROPY: i32 = 1The example for this item is shown with the ones above: ml-Trees.salam
TREE_MSE
const TREE_MSE: i32 = 2The example for this item is shown with the ones above: ml-Trees.salam
IMPUTE_MEAN
const IMPUTE_MEAN: i32 = 0The example for this item is shown with the ones above: ml-Prep.salam
IMPUTE_MEDIAN
const IMPUTE_MEDIAN: i32 = 1The example for this item is shown with the ones above: ml-Prep.salam
IMPUTE_CONSTANT
const IMPUTE_CONSTANT: i32 = 2The example for this item is shown with the ones above: ml-Prep.salam
ML_TOL
const ML_TOL: f64 = 1e-06The example for this item is shown with the ones above: ml-Prep.salam
LINK_SINGLE
const LINK_SINGLE: i32 = 0The example for this item is shown with the ones above: ml-Cluster.salam
LINK_COMPLETE
const LINK_COMPLETE: i32 = 1The example for this item is shown with the ones above: ml-Cluster.salam
LINK_AVERAGE
const LINK_AVERAGE: i32 = 2The example for this item is shown with the ones above: ml-Cluster.salam
KNN_BRUTE
const KNN_BRUTE: i32 = 0The example for this item is shown with the ones above: ml-Classify.salam
KNN_KDTREE
const KNN_KDTREE: i32 = 1The example for this item is shown with the ones above: ml-Classify.salam
KERNEL_LINEAR
const KERNEL_LINEAR: i32 = 0The example for this item is shown with the ones above: ml-Classify.salam
KERNEL_RBF
const KERNEL_RBF: i32 = 1The example for this item is shown with the ones above: ml-Classify.salam
KERNEL_POLY
const KERNEL_POLY: i32 = 2The example for this item is shown with the ones above: ml-Classify.salam
Functions
LabelsToInts
func LabelsToInts(y &: matrix.Matrix): Vector<int>The example for this item is shown with the ones above: ml-Prep.salam
CountClasses
func CountClasses(y &: matrix.Matrix): intThe 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): SplitThe example for this item is shown with the ones above: ml-Prep.salam
KFold
func KFold(n: int, k: int, shuffle: bool, seed: i64): FoldsThe example for this item is shown with the ones above: ml-Prep.salam
StratifiedKFold
func StratifiedKFold(y &: matrix.Matrix, k: int, seed: i64): FoldsThe 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 example for this item is shown with the ones above: ml-Select.salam
MeanScore
func MeanScore(scores &: Vector<f64>): f64The 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 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): GridResultThe example for this item is shown with the ones above: ml-Select.salam
SilhouetteScore
func SilhouetteScore(x &: matrix.Matrix, labels &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Cluster.salam
AdjustedRandIndex
func AdjustedRandIndex(a &: matrix.Matrix, b &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Cluster.salam
NewElasticNet
func NewElasticNet(alpha: f64, l1_ratio: f64): LassoThe example for this item is shown with the ones above: ml-Linear.salam
NewKDTree
func NewKDTree(x &: matrix.Matrix, leaf_size: int): KDTreeThe example for this item is shown with the ones above: ml-Trees.salam
Accuracy
func Accuracy(y &: matrix.Matrix, pred &: matrix.Matrix): f64The 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.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
Precision
func Precision(y &: matrix.Matrix, pred &: matrix.Matrix, positive: f64): f64The example for this item is shown with the ones above: ml-Classify.salam
Recall
func Recall(y &: matrix.Matrix, pred &: matrix.Matrix, positive: f64): f64The example for this item is shown with the ones above: ml-Classify.salam
F1
func F1(y &: matrix.Matrix, pred &: matrix.Matrix, positive: f64): f64The 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): f64The example for this item is shown with the ones above: ml-Classify.salam
RocAuc
func RocAuc(y &: matrix.Matrix, score &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
LogLoss
func LogLoss(y &: matrix.Matrix, proba &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
MSE
func MSE(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
MAE
func MAE(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
RMSE
func RMSE(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
R2Score
func R2Score(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
RocCurve
func RocCurve(y &: matrix.Matrix, score &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
PrecisionRecallCurve
func PrecisionRecallCurve(y &: matrix.Matrix, score &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: ml-Classify.salam
AveragePrecision
func AveragePrecision(y &: matrix.Matrix, score &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
BalancedAccuracy
func BalancedAccuracy(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
MatthewsCorrCoef
func MatthewsCorrCoef(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
CohenKappa
func CohenKappa(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Classify.salam
ExplainedVariance
func ExplainedVariance(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
MedianAbsoluteError
func MedianAbsoluteError(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam
MAPE
func MAPE(y &: matrix.Matrix, pred &: matrix.Matrix): f64The example for this item is shown with the ones above: ml-Linear.salam