Skip to content

Module_MachineLearning

cyclone_dll edited this page Sep 13, 2026 · 15 revisions

Location: Home / Machine learning

Machine learning module

factor analysis, principal component analysis (PCA), t-SNE dimensionality reduction, independent component analysis (ICA / FastICA) for blind source separation, and more.

Subsystem Namespace / types Description
Interfaces IClassifier<T>, IRegressor<T>, IPreprocessor<T> Unified contracts for classification, regression, preprocessing
Pipelines ClassificationPipeline<T>, RegressionPipeline<T> Preprocessor chain + model
Classification Classfication.*, KnnClassifier, NumericDecisionTree, NumericRandomForest Softmax, KNN, logistic regression, OvR, GNB, numeric trees/forests
Ensembles AdaBoostClassifier<T>, GradientBoostingClassifier<T> SAMME decision stumps, gradient boosting multiclass
Regression Regression.*, KnnRegressor Multivariate linear/ridge regression, KNN regression; MLR/MVR/KnnRegressor implement IBatchRegressor
Preprocessing Preprocessing.* StandardScaler, MinMaxScaler, LabelEncoder, OneHotEncoder
Evaluation EvaluationMetrics, ClassificationMetrics MSE/RMSE/accuracy, confusion matrix, P/R/F1
Utilities DataSplit, CrossValidation, GridSearch, ModelBatchExtensions, IBatchClassifier / IBatchRegressor Split, K-fold CV, grid search, batch prediction
Dimensionality reduction DimensionalityReduction.LDA, PCA<TSelf> LDA; PCA with out-of-sample Transform (0.10.18)
Source separation Decomposition.ICA FastICA blind source separation (log-cosh / exp / cube, symmetric / deflation)
Serialization Serialization.ModelJsonPersistence Scaler / Softmax / scaler-chain / kNN / RF / SVM / IsolationForest JSON persistence (IF Save/Load: 0.10.11)
Anomaly detection IsolationForest<T> Isolation Forest Fit + Score / Predict + CaptureSnapshot (persist: 0.10.11)

Quick examples

Classification pipeline

using Vorcyc.Mathematics.MachineLearning.Classfication;

var pipeline = new ClassificationPipeline<double>()
    .AddPreprocessor(new StandardScaler<double>())
    .SetClassifier(new SoftmaxRegression<double>(learningRate: 0.1, epochs: 2000));
pipeline.Fit(xTrain, yTrain);
int label = pipeline.Predict(sample);

Regression pipeline + K-fold cross-validation

using Vorcyc.Mathematics.MachineLearning.Regression;

IRegressor<double> model = new MultipleLinearRegression<double>();
var regPipe = new RegressionPipeline<double>()
    .AddPreprocessor(new StandardScaler<double>())
    .SetRegressor(model);
regPipe.Fit(x, y);
double[] yHat = regPipe.PredictBatch(x);

var cv = CrossValidation.ValidateRegressor(
    x, y, folds: 5, seed: 1,
    buildPredictor: (xTr, yTr) =>
    {
        var m = new MultivariateRidgeRegression<double>(0.01);
        m.Fit(xTr, yTr);
        return s => m.Predict(s);
    });

AdaBoost / gradient boosting

var ada = new AdaBoostClassifier<double>(nEstimators: 50);
ada.Fit(xTrain, yTrain);

var gbt = new GradientBoostingClassifier<double>(nEstimators: 50, learningRate: 0.1, maxDepth: 3);
gbt.Fit(xTrain, yTrain);

0.9 classifier walkthrough

The examples below use 2D numeric features and integer class labels (0, 1, 2), consistent with Examples/MachineLearning_example and TESTS/ML_module_test. All classifiers implement IClassifier<T> with Fit(x, y) / Predict(sample).

# Run full classification demo (default)
dotnet run --project Examples/MachineLearning_example

# Regression pipeline demo
dotnet run --project Examples/MachineLearning_example -- regression

Data split

using Vorcyc.Mathematics.MachineLearning;

// x: [samples, features], y: integer class labels
double[,] x = { /* ... */ };
int[] y = { /* ... */ };

var (xTrain, yTrain, xTest, yTest) = DataSplit.TrainTestSplit(x, y, testRatio: 0.25, seed: 7);

Numeric decision tree / random forest

var tree = new NumericDecisionTree<double>(maxDepth: 8);
tree.Fit(xTrain, yTrain);
int label = tree.Predict([2.1, 1.8]);

var forest = new NumericRandomForest<double>(
    numTrees: 80, maxFeatures: 2, maxDepth: 10, seed: 11);
forest.Fit(xTrain, yTrain);
label = forest.Predict([2.1, 1.8]);

Gaussian naive Bayes / KNN

using Vorcyc.Mathematics.MachineLearning.Classfication;

var gnb = new GaussianNaiveBayes<double>();
gnb.Fit(xTrain, yTrain);
label = gnb.Predict([2.1, 1.8]);

// Per-class posterior probabilities (class → probability)
var posteriors = gnb.PredictProbabilities([2.1, 1.8]);

var knn = new KnnClassifier<double>(k: 3);
knn.Fit(xTrain, yTrain);
label = knn.Predict([2.1, 1.8]);

var knnReg = new KnnRegressor<double>(k: 3);
knnReg.Fit(xTrainReg, yTrainReg);
double yHat = knnReg.Predict(sample);
double[] yBatch = knnReg.PredictBatch(xTestReg);

Softmax / logistic regression / one-vs-rest

using Vorcyc.Mathematics.MachineLearning.Classfication;

var softmax = new SoftmaxRegression<double>(learningRate: 0.1, epochs: 2500);
softmax.Fit(xTrain, yTrain);
label = softmax.Predict([2.1, 1.8]);

// Binary classification (labels must be 0 or 1)
var logit = new LogisticRegression<double>(learningRate: 0.05, epochs: 2000);
logit.Fit(xTrainBinary, yTrainBinary);

// Multiclass: trains one binary classifier per class
var ovr = new OneVsRestClassifier<double>(learningRate: 0.1, epochs: 2500);
ovr.Fit(xTrain, yTrain);
label = ovr.Predict([2.1, 1.8]);

Evaluation metrics

// Batch prediction (IBatchClassifier models use optimized paths: trees/forests, GNB, KNN, Softmax, logistic, OvR, pipelines, etc.)
int[] predictions = forest.PredictBatch(xTest);
// Or: var predictions = ModelBatchExtensions.PredictBatch(anyClassifier, xTest);

double accuracy = EvaluationMetrics.Accuracy(yTest, predictions);

var cm = ClassificationMetrics.ConfusionMatrix(yTest, predictions);
double macroF1 = ClassificationMetrics.MacroF1(cm);
double microF1 = ClassificationMetrics.MicroF1(cm);
double precision0 = ClassificationMetrics.Precision(cm, classLabel: 0);

Softmax / Logistic mini-batch training

using Vorcyc.Mathematics.MachineLearning.Classfication;

var softmax = new SoftmaxRegression<double>(
    learningRate: 0.1, epochs: 2000, batchSize: 32, seed: 42);
softmax.Fit(xTrain, yTrain);

var logit = new LogisticRegression<double>(
    learningRate: 0.05, epochs: 2000, batchSize: 32, seed: 42);
logit.Fit(xTrainBinary, yTrainBinary);

K-fold cross-validation

var cv = CrossValidation.Validate(
    x, y,
    folds: 5,
    seed: 21,
    buildPredictor: (xTr, yTr) =>
    {
        var model = new GaussianNaiveBayes<double>();
        model.Fit(xTr, yTr);
        return sample => model.Predict(sample);
    });

Console.WriteLine($"Accuracy: {cv.MeanAccuracy:P1} ± {cv.StdAccuracy:P1}");
Console.WriteLine($"Macro-F1: {cv.MeanMacroF1:F3} ± {cv.StdMacroF1:F3}");

Grid search (hyperparameters)

using Vorcyc.Mathematics.MachineLearning.Classfication;

var candidates = new[]
{
    new { Epochs = 1000, Lr = 0.05 },
    new { Epochs = 2000, Lr = 0.10 }
};

var result = GridSearch.SearchClassifier(
    x, y,
    candidates,
    opt => new SoftmaxRegression<double>(learningRate: opt.Lr, epochs: opt.Epochs),
    folds: 3,
    seed: 5);

// Retrain on full data with best hyperparameters
var bestOpts = result.BestOptions;
var bestModel = new SoftmaxRegression<double>(learningRate: bestOpts.Lr, epochs: bestOpts.Epochs);
bestModel.Fit(x, y);
label = bestModel.Predict([2.1, 1.8]);

JSON persistence (scaler + Softmax + kNN / RF / SVM, 0.10.10)

using Vorcyc.Mathematics.MachineLearning.Classfication;
using Vorcyc.Mathematics.MachineLearning.Preprocessing;
using Vorcyc.Mathematics.MachineLearning.Serialization;

var scaler = new StandardScaler<double>();
scaler.Fit(xTrain);
ModelJsonPersistence.SaveStandardScaler(scaler, "scaler.json");

var xScaled = scaler.Transform(xTrain);
var model = new SoftmaxRegression<double>(learningRate: 0.1, epochs: 2000);
model.Fit(xScaled, yTrain);
ModelJsonPersistence.SaveSoftmaxRegression(model, "softmax.json");

// Inference after load
var loadedScaler = ModelJsonPersistence.LoadStandardScaler("scaler.json");
var loadedModel = ModelJsonPersistence.LoadSoftmaxRegression("softmax.json");
label = loadedModel.Predict(loadedScaler.Transform([2.1, 1.8]));

// Also available (0.10.10): Save/LoadStandardScalerChain, Save/LoadKnnClassifier,
// Save/LoadNumericRandomForest, Save/LoadSupportVectorMachine
// Also available (0.10.11): Save/LoadIsolationForest

Gaussian process with predictive std (0.10.10)

using Vorcyc.Mathematics.MachineLearning.CurveFitting;

var gpr = CurveFitter<float>.GaussianProcess(xs, ys, lengthScale: 1f, signalVariance: 1f, noiseVariance: 0.01f);
float mean = gpr.PredictMean(xStar);   // or gpr.Predict(xStar)
float std  = gpr.PredictStd(xStar);    // band: mean ± k * std
float var  = gpr.PredictVariance(xStar);

0.10.17: classic CurveFitter methods (Linear / Polynomial / Exponential / Logarithmic / Power / Sinusoidal / CubicSpline / LOWESS / MovingAverage / Nonlinear / BayesianLinear) take optional CancellationToken. GPR already did. Cubic spline Predict is binary search.

0.10.16: CurveFitter Polynomial / Exponential / Logarithmic / Power Fit_SIMD paths hoist stackalloc out of hot loops, avoiding large-N (~≥5×10⁵) StackOverflow under Auto/Simd. Linear SIMD was already safe.

Isolation Forest anomaly scores (0.10.10) + JSON snapshot (0.10.11)

using Vorcyc.Mathematics.MachineLearning;
using Vorcyc.Mathematics.MachineLearning.Serialization;

var forest = new IsolationForest<float>(numTrees: 100, subsampleSize: 256, seed: 42);
forest.Fit(featureMatrix);                 // rows = samples
float[] scores = forest.Score(featureMatrix); // higher ≈ more anomalous
int[] flags = forest.Predict(featureMatrix, threshold: 0.6f); // 1 = anomaly
ModelJsonPersistence.SaveIsolationForest(forest, "iforest.json"); // 0.10.11
var loaded = ModelJsonPersistence.LoadIsolationForest("iforest.json");

Linear discriminant analysis (LDA)

using Vorcyc.Mathematics.MachineLearning.DimensionalityReduction;

var lda = new LinearDiscriminantAnalysis<double>(numComponents: 1);
lda.Fit(x, y);
label = lda.Predict([2.1, 1.8]);
var projected = lda.Transform([2.1, 1.8]); // reduced features

ComputingContext integration (parallel execution)

Most estimators accept an optional ComputingContext? context constructor argument and/or expose a settable Context property, letting you opt into SIMD/parallel CPU execution per model, per scope, or process-wide. Resolution order is explicit context → ComputingScope → ComputingContext.Default; null means "unspecified" (it still follows the chain), not "force scalar". See the ComputingContext module for presets and thresholds.

What is parallelized

Area Types Parallel work
Batch prediction LogisticRegression, SoftmaxRegression, GaussianNaiveBayes, OneVsRestClassifier, NumericDecisionTree, NumericRandomForest, KnnClassifier, KnnRegressor, MultipleLinearRegression, MultivariateRidgeRegression, ClassificationPipeline, RegressionPipeline PredictBatch over rows (via IBatchClassifier / IBatchRegressor)
Training / fit GradientBoostingClassifier, OneVsRestClassifier, NumericRandomForest per-class / per-tree fit
Clustering KMeansClusterer, ExpectationMaximization / GMM, DBSCAN, HierarchicalClustering, VectorQuantization assignment / E-step / neighbor / linkage passes
Dimensionality reduction & separation PCA, LinearDiscriminantAnalysis, FactorAnalysis, TSNE, Decomposition.ICA covariance / projection / affinity / whitening passes
Metrics & helpers ClassificationMetrics.ConfusionMatrix(..., context), ModelBatchExtensions.PredictBatch(..., context) histogram / generic batch prediction
Curve fitting CurveFitter<T> (Linear … Power, Sinusoidal, CubicSpline, LocallyWeighted, MovingAverage, GaussianProcess, Nonlinear, BayesianLinear) optional computingContext; Statistics-style Normal / Simd / Parallel dispatch (see ComputingContext); GaussianProcess accepts CancellationToken (0.10.9) and returns GaussianProcessFitResult with PredictStd / PredictVariance (0.10.10)

Every parallel kernel writes only to disjoint indices with thread-local scratch buffers, so parallel results are identical to the serial path (see TESTS/ML_module_test).

AdaBoostClassifier and SupportVectorMachine accept context and expose Context for API uniformity, but their current fit/predict run serially (reserved for a future parallel path).

using Vorcyc.Mathematics;                 // ComputingContext, ComputingScope
using Vorcyc.Mathematics.MachineLearning;

// 1) Per-model: set the policy on the estimator instance
var forest = new NumericRandomForest<double>(numTrees: 80, maxFeatures: 2, maxDepth: 10, seed: 11)
{
    Context = ComputingContext.Parallel
};
forest.Fit(xTrain, yTrain);
int[] predictions = forest.PredictBatch(xTest);   // batch prediction over rows runs in parallel

// 2) Per-scope: leave Context unset (null) and govern several operations at once
var knn = new KnnClassifier<double>(k: 3);
knn.Fit(xTrain, yTrain);
using (ComputingScope.Enter(ComputingContext.Parallel))
{
    int[] p = knn.PredictBatch(xTest);                        // honors the scope
    var cm = ClassificationMetrics.ConfusionMatrix(yTest, p); // honors the scope
}

For models implementing IBatchClassifier / IBatchRegressor, the instance Context governs PredictBatch; the context argument of ModelBatchExtensions.PredictBatch only applies to the generic fallback used by other classifiers.


The types below live in the Vorcyc.Mathematics.MachineLearning namespace and its sub-namespaces (.Regression, .Classfication, .Clustering, .DimensionalityReduction, .Decomposition, .Distances, .Preprocessing, .Serialization, .CurveFitting).

📒 Contents


Vorcyc.Mathematics.MachineLearning.KNN<T> class

KNN<T> is for incremental K-nearest-neighbor regression (Add / Regress / RegressBatch); for matrix training use KnnRegressor<T> (IRegressor + IBatchRegressor). For classification use KnnClassifier<T>.

Properties

1. Task

  • public MachineLearningTask Task { get; }
    • Fixed to MachineLearningTask.Regression。

Constructors

1. KNN

  • public KNN(ComputingContext? context = null)
    • Initializes KNN class instance.
    • Parameters:
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Add

  • public void Add(Point<T> point, T target) / Add(ReadOnlySpan<T> features, T target)
    • Adds a regression sample.

2. Regress

  • public T Regress(Point<T> point, int k) / Regress(ReadOnlySpan<T> features, int k, bool distanceWeighted = false)
    • KNN regression on a feature vector; optional distance weighting.

3. RegressBatch

  • public T[] RegressBatch(T[,] x, int k, bool distanceWeighted = false)
    • Batch KNN regression for each row of the feature matrix; returns one prediction per row.
  • public void RegressBatch(T[,] x, int k, Span<T> predictions, bool distanceWeighted = false)
    • Writes batch KNN regression predictions into the supplied span.

Code example

using Vorcyc.Mathematics.MachineLearning;
using Vorcyc.Mathematics.Numerics;

var knn = new KNN<double>();
knn.Add(new Point<double>(1.0, 0.0), 2.0);
knn.Add(new Point<double>(2.0, 0.0), 4.0);
knn.Add(new Point<double>(3.0, 0.0), 6.0);
double y = knn.Regress(new Point<double>(2.5, 0.0), k: 2);

// Classification
var clf = new KnnClassifier<double>(k: 3);
clf.Fit(xTrain, yTrain);
int label = clf.Predict(sample);

Vorcyc.Mathematics.MachineLearning.MultipleLinearRegression<T> class

MultipleLinearRegression<T> fits multivariate linear regression. The normal equations use Cholesky (LinearEquationSolver.CholeskySolve) when positive definite, otherwise LU.

Properties

1. Coefficients

  • public IReadOnlyList<T> Coefficients { get; }
    • Gets regression coefficients.

2. Intercept

  • public T Intercept { get; }
    • Gets regression intercept.

3. RSquared

  • public T RSquared { get; }
    • Gets the coefficient of determination R².

4. Context

  • public ComputingContext? Context { get; set; }
    • Execution policy honored by batch prediction; when null the ambient scope or default context is used.

5. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports regression.

Methods

1. Fit

  • public void Fit(T[,] x, T[] y)
    • Fits coefficients and intercept.
    • Parameters:
      • x: Independent variable matrix.
      • y: Dependent variable vector.

2. Predict

  • public T Predict(T[] x)
    • Predicts the dependent variable from inputs and coefficients.
    • Parameters:
      • x: Independent variables.
    • Returns: Predicted dependent value.

Code example

The following example uses MultipleLinearRegression<T> with comments:

using System;
using Vorcyc.Mathematics.MachineLearning;
using Vorcyc.Mathematics.MachineLearning.Regression;

public class MultipleLinearRegressionExample
{
    public static void Main()
    {
        // Independent variable matrix
        double[,] x = new double[,] { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 }, { 5, 6 } };
        // Dependent variable vector
        double[] y = new double[] { 2, 3, 5, 7, 11 };

        // Create multiple linear regression instance
        var regression = new MultipleLinearRegression<double>();
        regression.Fit(x, y);

        // Print intercept and coefficients
        Console.WriteLine("Intercept: " + regression.Intercept);
        Console.WriteLine("Coefficients:");
        for (int i = 0; i < regression.Coefficients.Count; i++)
        {
            Console.WriteLine("Coefficient " + (i + 1) + ": " + regression.Coefficients[i]);
        }

        // Predict for new input
        double[] newInput = new double[] { 6, 7 };
        double prediction = regression.Predict(newInput);
        Console.WriteLine("Predicted value for input " + string.Join(", ", newInput) + ": " + prediction);
    }
}

Vorcyc.Mathematics.MachineLearning.SimpleLinearRegression<T> class

SimpleLinearRegression<T> performs simple (univariate) linear regression.

Properties

1. Slope

  • public T Slope { get; }
    • Gets linear regression slope.

2. Intercept

  • public T Intercept { get; }
    • Gets linear regression intercept.

3. RSquared

  • public T RSquared { get; }
    • Gets the coefficient of determination R².

4. Context

  • public ComputingContext? Context { get; set; }
    • Execution policy honored by PredictBatch; when null the ambient scope or default context is used.

5. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports regression.

Methods

1. Fit

  • public (T slope, T intercept) Fit(Point<T>[] data)

    • Computes slope and intercept.
    • Parameters:
      • data: Array of data points.
    • Returns: Tuple of slope and intercept.
  • public (T slope, T intercept) Fit(Span<T> x, Span<T> y)

    • Computes slope and intercept.
    • Parameters:
      • x: Independent variable array.
      • y: Dependent variable array.
    • Returns: Tuple of slope and intercept.

2. GetX

  • public T GetX(T y)
    • Computes x from y, slope, and intercept.
    • Parameters:
      • y: Dependent variable value.
    • Returns: Computed x.

3. GetY

  • public T GetY(T x)
    • Computes y from x, slope, and intercept.
    • Parameters:
      • x: Independent variable value.
    • Returns: Computed y.

4. PredictBatch

  • public T[] PredictBatch(Span<T> x, ComputingContext? context = null)
    • Predicts y for a batch of x values; honors Context for SIMD/parallel execution.
    • Parameters:
      • x: Independent variable values to evaluate.
      • context: Optional execution policy overriding Context.
    • Returns: Predicted y values, one per element of x.

Code example

The following example uses SimpleLinearRegression<T> with comments:

using System;
using Vorcyc.Mathematics.MachineLearning;
using Vorcyc.Mathematics.MachineLearning.Regression;
using Vorcyc.Mathematics.Numerics;
using static System.Runtime.InteropServices.JavaScript.JSType;
public class SimpleLinearRegressionExample
{
    public static void Main()
    {
        // Data points
        var data = new Point<double>[] { new Point<double>(1.0, 2.0), new Point<double>(2.0, 3.0), new Point<double>(3.0, 5.0), new Point<double>(4.0, 4.0), new Point<double>(5.0, 6.0) };
        // Create simple linear regression instance
        var regression = new SimpleLinearRegression<double>();
        var (slope, intercept) = regression.Fit(data);

        // Print slope and intercept
        Console.WriteLine($"Slope: {slope}, Intercept: {intercept}");

        // Predict y for new x
        var x = 6.0;
        var y = regression.GetY(x);
        Console.WriteLine($"For x = {x}, predicted y = {y}");
    }
}

Vorcyc.Mathematics.MachineLearning.SupportVectorMachine<TSelf> class

SupportVectorMachine<TSelf> is a simple linear SVM with selectable kernels.

Properties

1. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports classification and regression.

Constructors

1. SupportVectorMachine

  • public SupportVectorMachine(int featureCount, TSelf? learningRate = null, int epochs = 1000, SupportVectorMachineKernelType kernelType = SupportVectorMachineKernelType.Linear, TSelf? gamma = null, int polynomialDegree = 3, TSelf? sigmoidAlpha = null, TSelf? sigmoidConstant = null, ComputingContext? context = null)
    • Initializes SupportVectorMachine class instance.
    • Parameters:
      • featureCount: Number of input features.
      • learningRate: Learning rate; default 0.01 when null.
      • epochs: Training epochs.
      • kernelType: Kernel type; default linear. Values: Linear, Polynomial, Gaussian, RBF, DotProduct, Sigmoid.
      • gamma: Kernel gamma; default 1.0 when null (used by polynomial / Gaussian / RBF).
      • polynomialDegree: Degree of the polynomial kernel; default 3.
      • sigmoidAlpha: Sigmoid kernel alpha; default 0.01 when null.
      • sigmoidConstant: Sigmoid kernel constant; default 1.0 when null.
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Train

  • public void Train(TSelf[][] inputs, int[] outputs)
    • Trains SVM on provided data.
    • Parameters:
      • inputs: Training inputs (feature arrays).
      • outputs: Output labels.

2. Predict

  • public int Predict(Span<TSelf> input)
    • Predicts class label for input.
    • Parameters:
      • input: Feature array.
    • Returns: Predicted label (1 or -1).

3. PredictRaw

  • private TSelf PredictRaw(TSelf[] input)
    • Raw prediction score.
    • Parameters:
      • input: Feature array.
    • Returns: Raw prediction score.

4. GetKernelFunction

  • private Func<TSelf[], TSelf[], TSelf> GetKernelFunction(SupportVectorMachineKernelType kernelType)
    • Gets the kernel function for the specified type.
    • Parameters:
      • kernelType: Kernel type。
    • Returns: Kernel delegate.

5. LinearKernel

  • private static TSelf LinearKernel(TSelf[] x, TSelf[] y)
    • Linear kernel function.
    • Parameters:
      • x: First input vector。
      • y: Second input vector。
    • Returns: Kernel result.

6. PolynomialKernel

  • private static TSelf PolynomialKernel(TSelf[] x, TSelf[] y)
    • Polynomial kernel function.
    • Parameters:
      • x: First input vector。
      • y: Second input vector。
    • Returns: Kernel result.

7. GaussianKernel

  • private static TSelf GaussianKernel(TSelf[] x, TSelf[] y)
    • Gaussian (RBF) kernel function.
    • Parameters:
      • x: First input vector。
      • y: Second input vector。
    • Returns: Kernel result.

8. SigmoidKernel

  • private static TSelf SigmoidKernel(TSelf[] x, TSelf[] y)
    • Sigmoid kernel function.
    • Parameters:
      • x: First input vector。
      • y: Second input vector。
    • Returns: Kernel result.

Code example

The following example uses SupportVectorMachine<TSelf> with comments:

using System;
using Vorcyc.Mathematics.MachineLearning;
public class SupportVectorMachineExample
{
    public static void Main()
    {
        // Training data
        double[][] inputs = new double[][] { new double[] { 1.0, 2.0 }, new double[] { 2.0, 3.0 }, new double[] { 3.0, 3.0 }, new double[] { 6.0, 5.0 }, new double[] { 7.0, 8.0 } };
        int[] outputs = new int[] { 1, 1, -1, -1, 1 };
        // Create SVM instance
        var svm = new SupportVectorMachine<double>(2);

        // Train SVM
        svm.Train(inputs, outputs);

        // Predict label for new input
        double[] newInput = new double[] { 4.0, 4.0 };
        int prediction = svm.Predict(newInput);
        Console.WriteLine($"Prediction for input {string.Join(", ", newInput)}: {prediction}");
    }
}

Vorcyc.Mathematics.MachineLearning.Clustering.DBSCAN<T> class

DBSCAN<T> performs DBSCAN clustering on 2D points.

Properties

1. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports clustering.

Constructors

1. DBSCAN

  • public DBSCAN(Point<T>[] points, T eps, int minPts, ComputingContext? context = null)
    • Initializes DBSCAN with points, radius, and minimum points.
    • Parameters:
      • points: Points to cluster.
      • eps: Neighborhood radius.
      • minPts: Minimum points to form a cluster.
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Cluster

  • public List<List<Point<T>>> Cluster()
    • Runs DBSCAN clustering.
    • Returns: List of clusters (each a list of points).

2. ExpandCluster

  • private void ExpandCluster(Point<T> point, List<Point<T>> neighbors, List<Point<T>> cluster)
    • Expands a cluster.
    • Parameters:
      • point: Current point.
      • neighbors: Neighbors of the current point.
      • cluster: Current cluster.

3. GetNeighbors

  • private List<Point<T>> GetNeighbors(Point<T> point)
    • Gets neighbors of a point.
    • Parameters:
      • point: Current point.
    • Returns: List of neighbors.

Code example

The following example uses DBSCAN<T> with comments:

using System;
using System.Collections.Generic;
using Vorcyc.Mathematics.MachineLearning.Clustering;
using Vorcyc.Mathematics.Numerics;
public class DBSCANExample
{
    public static void Main()
    { 
        
        // Points
        var points = new Point<double>[] { new Point<double>(1.0, 2.0), new Point<double>(2.0, 2.0), new Point<double>(2.0, 3.0), new Point<double>(8.0, 7.0), new Point<double>(8.0, 8.0), new Point<double>(25.0, 80.0) };
        // Create DBSCAN instance
        var dbscan = new DBSCAN<double>(points, 3.0, 2);

        // Run clustering
        var clusters = dbscan.Cluster();

        // Print clusters
        for (int i = 0; i < clusters.Count; i++)
        {
            Console.WriteLine($"Cluster {i + 1}:");
            foreach (var point in clusters[i])
            {
                Console.WriteLine($"({point.X}, {point.Y})");
            }
        }
    }
}

Vorcyc.Mathematics.MachineLearning.Clustering.ExpectationMaximization<T> class

ExpectationMaximization<T> implements the expectation-maximization (EM) algorithm.

Properties

1. Means

  • public IReadOnlyList<T[]> Means { get; }
    • Gets cluster centers (means).

2. Covariances

  • public IReadOnlyList<Matrix<T>> Covariances { get; }
    • Gets covariance matrices.

3. Weights

  • public IReadOnlyList<T> Weights { get; }
    • Gets weights.

4. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports clustering.

Constructors

1. ExpectationMaximization

  • public ExpectationMaximization(int numClusters)
    • Initializes ExpectationMaximization class instance。
    • Parameters:
      • numClusters: Number of clusters.

Methods

1. Fit

  • public void Fit(List<T[]> data, int maxIterations = 100, T tolerance = default)
    • Fits data with EM.
    • Parameters:
      • data: Data as a list of arrays.
      • maxIterations: Maximum iterations; default 100.
      • tolerance: Convergence tolerance; defaults to 1e-4 when default.

2. Predict

  • public int Predict(T[] dataPoint)
    • Predicts the cluster index of a data point.
    • Parameters:
      • dataPoint: Data point to predict.
    • Returns: Index of the cluster to which the data point belongs.

Code example

The following example uses ExpectationMaximization<T> with comments:

using System;
using System.Collections.Generic;
using Vorcyc.Mathematics.MachineLearning.Clustering;
public class ExpectationMaximizationExample
{
    public static void Main()
    { 
        // Data points
        var data = new List<double[]>
        {
            new double[] { 1.0, 2.0 },
            new double[] { 2.0, 2.0 },
            new double[] { 2.0, 3.0 },
            new double[] { 8.0, 7.0 },
            new double[] { 8.0, 8.0 },
            new double[] { 25.0, 80.0 }
        };
        // Create EM instance
        var em = new ExpectationMaximization<double>(2);

        // Fit data
        em.Fit(data);

        // Print cluster centers
        Console.WriteLine("Cluster Centers (Means):");
        foreach (var mean in em.Means)
        {
            Console.WriteLine($"({string.Join(", ", mean)})");
        }

        // Print covariance matrices
        Console.WriteLine("Covariance Matrices:");
        foreach (var covariance in em.Covariances)
        {
            for (int i = 0; i < covariance.Rows; i++)
            {
                for (int j = 0; j < covariance.Columns; j++)
                {
                    Console.Write($"{covariance[i, j]} ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        // Print weights
        Console.WriteLine("Weights:");
        foreach (var weight in em.Weights)
        {
            Console.WriteLine(weight);
        }
    }
}

Vorcyc.Mathematics.MachineLearning.Clustering.GMM<T> class

GMM<T> implements a Gaussian mixture model.

Properties

1. Means

  • public IReadOnlyList<T[]> Means { get; }
    • Gets cluster centers (means).

2. Covariances

  • public IReadOnlyList<Matrix<T>> Covariances { get; }
    • Gets covariance matrices.

3. Weights

  • public IReadOnlyList<T> Weights { get; }
    • Gets weights.

4. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports clustering.

Constructors

1. GMM

  • public GMM(int numComponents, int maxIterations = 100, T tolerance = default, ComputingContext? context = null)
    • Initializes GMM class instance.
    • Parameters:
      • numComponents: Number of Gaussian components.
      • maxIterations: Maximum iterations.
      • tolerance: Log-likelihood convergence tolerance; defaults to 1e-6 when default.
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Fit

  • public void Fit(List<T[]> data)
    • Fits data with GMM.
    • Parameters:
      • data: Data as a list of arrays.

2. Predict

  • public int Predict(T[] dataPoint)
    • Predicts the cluster index of a data point.
    • Parameters:
      • dataPoint: Data point to predict.
    • Returns: Index of the cluster to which the data point belongs.

Code example

The following example uses GMM<T> with comments:

using System;
using System.Collections.Generic;
using Vorcyc.Mathematics.MachineLearning.Clustering;
public class GMMExample
{
    public static void Main()
    {
        // Data points
        var data = new List<double[]>
        {
            new double[] { 1.0, 2.0 },
            new double[] { 2.0, 2.0 },
            new double[] { 2.0, 3.0 },
            new double[] { 8.0, 7.0 },
            new double[] { 8.0, 8.0 },
            new double[] { 25.0, 80.0 }
        };
        // Create GMM instance
        var gmm = new GMM<double>(2);

        // Fit data
        gmm.Fit(data);

        // Print cluster centers
        Console.WriteLine("Cluster Centers (Means):");
        foreach (var mean in gmm.Means)
        {
            Console.WriteLine($"({string.Join(", ", mean)})");
        }

        // Print covariance matrices
        Console.WriteLine("Covariance Matrices:");
        foreach (var covariance in gmm.Covariances)
        {
            for (int i = 0; i < covariance.Rows; i++)
            {
                for (int j = 0; j < covariance.Columns; j++)
                {
                    Console.Write($"{covariance[i, j]} ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        // Print weights
        Console.WriteLine("Weights:");
        foreach (var weight in gmm.Weights)
        {
            Console.WriteLine(weight);
        }
    }
}

Vorcyc.Mathematics.MachineLearning.Clustering.HierarchicalClustering<T> class

HierarchicalClustering<T> performs hierarchical clustering on 2D points.

Properties

1. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports clustering.

Constructors

1. HierarchicalClustering

  • public HierarchicalClustering(Point<T>[] points, ComputingContext? context = null)
    • Initializes HierarchicalClustering with points.
    • Parameters:
      • points: Points to cluster.
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Cluster

  • public List<List<Point<T>>> Cluster(int k)
    • Hierarchical clustering until k clusters remain.
    • Parameters:
      • k: Desired number of clusters.
    • Returns: List of clusters (each a list of points).

2. FindClosestClusters

  • private (int cluster1, int cluster2, T minDistance) FindClosestClusters()
    • Finds the two closest clusters.
    • Returns: Indices of closest clusters and distance.

3. AverageLinkage

  • private T AverageLinkage(List<Point<T>> cluster1, List<Point<T>> cluster2)
    • Average linkage distance between two clusters.
    • Parameters:
      • cluster1: First cluster.
      • cluster2: Second cluster.
    • Returns: Average linkage distance between two clusters.

4. Distance

  • private T Distance(Point<T> a, Point<T> b)
    • Computes Euclidean distance between two points.
    • Parameters:
      • a: First point.
      • b: Second point.
    • Returns: Euclidean distance between two points.

Code example

The following example uses HierarchicalClustering<T> with comments:

using System;
using System.Collections.Generic;
using Vorcyc.Mathematics.MachineLearning.Clustering;
using Vorcyc.Mathematics.Numerics;
public class HierarchicalClusteringExample
{
    public static void Main()
    {
        // Points
        var points = new Point<double>[]
        {
            new Point<double>(1.0, 2.0),
            new Point<double>(2.0, 2.0),
            new Point<double>(2.0, 3.0),
            new Point<double>(8.0, 7.0),
            new Point<double>(8.0, 8.0),
            new Point<double>(25.0, 80.0)
        };
        // Create hierarchical clustering instance
        var hierarchicalClustering = new HierarchicalClustering<double>(points);

        // Run clustering
        var clusters = hierarchicalClustering.Cluster(2);

        // Print clusters
        for (int i = 0; i < clusters.Count; i++)
        {
            Console.WriteLine($"Cluster {i + 1}:");
            foreach (var point in clusters[i])
            {
                Console.WriteLine($"({point.X}, {point.Y})");
            }
        }
    }
}

Vorcyc.Mathematics.MachineLearning.Clustering.KMeansClusterer<T> class

KMeansClusterer<T> implements K-means clustering.

Properties

1. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports clustering.

Constructors

1. KMeansClusterer

  • public KMeansClusterer(T[][] data, int numClusters, int maxIterations, T tolerance, ComputingContext? context = null)
    • Initializes KMeansClusterer with data, cluster count, max iterations, and tolerance.
    • Parameters:
      • data: Data to cluster.
      • numClusters: Number of clusters.
      • maxIterations: Maximum iterations.
      • tolerance: Convergence tolerance.
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Cluster

  • public int[] Cluster()
    • Runs K-means clustering.
    • Returns: Cluster assignment per point.

2. InitClustering

  • private int[] InitClustering(int numData, int numClusters)
    • Initializes cluster assignments.
    • Parameters:
      • numData: Number of data points.
      • numClusters: Number of clusters.
    • Returns: Initial cluster assignments.

3. Allocate

  • private T[][] Allocate(int numRows, int numCols)
    • Allocates a 2D array.
    • Parameters:
      • numRows: Row count.
      • numCols: Column count.
    • Returns: Allocated 2D array.

4. UpdateCentroids

  • private bool UpdateCentroids()
    • Updates centroids.
    • Returns: Whether centroids changed.

5. UpdateClustering

  • private bool UpdateClustering()
    • Updates cluster assignments.
    • Returns: Whether assignments changed.

6. Distance

  • private T Distance(T[] vector1, T[] vector2)
    • Computes Euclidean distance between two vectors.
    • Parameters:
      • vector1: First vector.
      • vector2: Second vector.
    • Returns: Euclidean distance between two vectors.

7. MinIndex

  • private int MinIndex(T[] distances)
    • Index of minimum value in an array.
    • Parameters:
      • distances: Distance array.
    • Returns: Index of minimum.

Code example

The following example uses KMeansClusterer<T> with comments:

using System;
using Vorcyc.Mathematics.MachineLearning.Clustering;
public class KMeansClustererExample
{
    public static void Main()
    {
        // Data points
        var data = new double[][]
        {
            new double[] { 1.0, 2.0 },
            new double[] { 2.0, 2.0 },
            new double[] { 2.0, 3.0 },
            new double[] { 8.0, 7.0 },
            new double[] { 8.0, 8.0 },
            new double[] { 25.0, 80.0 }
        };
        // Create KMeansClusterer instance
        var kMeans = new KMeansClusterer<double>(data, 2, 100, 1e-6);

        // Run clustering
        var clustering = kMeans.Cluster();

        // Print clusters
        for (int i = 0; i < clustering.Length; i++)
        {
            Console.WriteLine($"Data point {i + 1} is in cluster {clustering[i] + 1}");
        }
    }
}

Vorcyc.Mathematics.MachineLearning.Clustering.VectorQuantization<T> class

VectorQuantization<T> implements vector quantization.

Properties

1. Codebook

  • public IReadOnlyList<T[]> Codebook { get; }
    • Gets codebook.

2. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports clustering.

Constructors

1. VectorQuantization

  • public VectorQuantization(int codebookSize, int dimensions)
    • Initializes VectorQuantization class instance。
    • Parameters:
      • codebookSize: Codebook size.
      • dimensions: Vector dimensions.

Methods

1. Train

  • public List<T> Train(IEnumerable<T[]> data, int maxIterations = 100)
    • Trains vector quantization model.
    • Parameters:
      • data: Training vectors (each an array of elements).
      • maxIterations: Maximum iterations.
    • Returns: Per-iteration errors.

2. Predict

  • public T[] Predict(T[] vector)
    • Finds nearest code vector to input.
    • Parameters:
      • vector: Input vector.
    • Returns: Nearest code vector.

3. SaveCodebook

  • public void SaveCodebook(string filePath)
    • Saves codebook to file.
    • Parameters:
      • filePath: File path.

4. LoadCodebook

  • public void LoadCodebook(string filePath)
    • Loads codebook from file.
    • Parameters:
      • filePath: File path.

Code example

The following example uses VectorQuantization<T> with comments:

using System;
using System.Collections.Generic;
using Vorcyc.Mathematics.MachineLearning.Clustering;
using Vorcyc.Mathematics.LinearAlgebra;
using System.Numerics;
public class VectorQuantizationExample
{
    public static void Main()
    {
        // Data points
        var data = new List<double[]>
        {
            new double[] { 1.0, 2.0 },
            new double[] { 2.0, 2.0 },
            new double[] { 2.0, 3.0 },
            new double[] { 8.0, 7.0 },
            new double[] { 8.0, 8.0 },
            new double[] { 25.0, 80.0 }
        };
        // Create vector quantization instance
        var vq = new VectorQuantization<double>(2, 2);

        // Train model
        var errors = vq.Train(data);

        // Print training errors
        Console.WriteLine("Training Errors:");
        foreach (var error in errors)
        {
            Console.WriteLine(error);
        }

        // Predict for new vector
        var newVector = new double[] { 3.0, 3.0 };
        var nearestCodeVector = vq.Predict(newVector);
        Console.WriteLine($"Nearest code vector to ({string.Join(", ", newVector)}): ({string.Join(", ", nearestCodeVector)})");

        // Save codebook
        vq.SaveCodebook("codebook.json");

        // Load codebook
        vq.LoadCodebook("codebook.json");
    }
}

Vorcyc.Mathematics.MachineLearning.DimensionalityReduction.FactorAnalysis<T> class

FactorAnalysis<T> performs factor analysis.

Properties

1. Loadings

  • public Matrix<T> Loadings { get; private set; }
    • Gets factor loadings matrix.

2. Communalities

  • public T[] Communalities { get; private set; }
    • Gets communalities array.

3. SpecificVariances

  • public T[] SpecificVariances { get; private set; }
    • Gets specific variances array.

4. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports dimensionality reduction.

Constructors

1. FactorAnalysis

  • public FactorAnalysis(ComputingContext? context = null)
    • Initializes FactorAnalysis class instance.
    • Parameters:
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Analyze

  • public void Analyze(Matrix<T> data, int numFactors)
    • Runs factor analysis.
    • Parameters:
      • data: Data matrix (rows = samples, columns = variables).
      • numFactors: Number of factors.

Code example

The following example uses FactorAnalysis<T> with comments:

using System;
using Vorcyc.Mathematics.MachineLearning.DimensionalityReduction;
using Vorcyc.Mathematics.LinearAlgebra;
public class FactorAnalysisExample
{
    public static void Main()
    {
        // Data matrix
        var data = new Matrix<double>(
            new double[,] 
            {
                { 1.0, 2.0, 3.0 }, 
                { 2.0, 3.0, 4.0 }, 
                { 3.0, 4.0, 5.0 }, 
                { 4.0, 5.0, 6.0 }, 
                { 5.0, 6.0, 7.0 } 
            });
        // Create factor analysis instance
        var factorAnalysis = new FactorAnalysis<double>();

        // Run factor analysis
        factorAnalysis.Analyze(data, 2);

        // Print factor loadings
        Console.WriteLine("Factor Loadings:");
        for (int i = 0; i < factorAnalysis.Loadings.Rows; i++)
        {
            for (int j = 0; j < factorAnalysis.Loadings.Columns; j++)
            {
                Console.Write($"{factorAnalysis.Loadings[i, j]} ");
            }
            Console.WriteLine();
        }

        // Print communalities
        Console.WriteLine("Communalities:");
        foreach (var communality in factorAnalysis.Communalities)
        {
            Console.WriteLine(communality);
        }

        // Print specific variances
        Console.WriteLine("Specific Variances:");
        foreach (var specificVariance in factorAnalysis.SpecificVariances)
        {
            Console.WriteLine(specificVariance);
        }
    }
}

Vorcyc.Mathematics.MachineLearning.DimensionalityReduction.PCA<TSelf> class

PCA<TSelf> performs principal component analysis for dimensionality reduction and feature extraction.

Properties

1. Task

  • public MachineLearningTask Task { get; }
    • Task type; supports dimensionality reduction.

2. Means / Eigenvalues / Eigenvectors (0.10.18)

  • public TSelf[] Means { get; }
    • Per-feature means used to center training and out-of-sample rows.
  • public TSelf[] Eigenvalues { get; }
    • Covariance eigenvalues, already sorted descending.
  • public TSelf[][] Eigenvectors { get; }
    • Principal axes; index j is component j.

Constructors

1. PCA

  • public PCA(TSelf[,] data, ComputingContext? context = null)
    • Initializes PCA class instance.
    • Parameters:
      • data: Input data (rows = samples, columns = features).
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Transform

  • public TSelf[,] Transform()
    • Transforms the (already centered) training data to principal components.
    • Returns: Transformed principal components.

2. Transform (out-of-sample, 0.10.18)

  • public TSelf[,] Transform(TSelf[,] x, int? components = null)
    • Centers x with the fitted means and projects onto the first components axes (all of them when null).

3. GetExplainedVarianceRatio

  • public TSelf[] GetExplainedVarianceRatio()
    • Gets explained variance ratios.
    • Returns: Explained variance ratio array.

Code example

The following example uses PCA<TSelf> with comments:

using System;
using Vorcyc.Mathematics.MachineLearning.DimensionalityReduction;
public class PCAExample
{
    public static void Main()
    {
        // Data matrix
        var data = new double[,]
        {
          { 1.0, 2.0, 3.0 },
          { 2.0, 3.0, 4.0 },
          { 3.0, 4.0, 5.0 },
          { 4.0, 5.0, 6.0 },
          { 5.0, 6.0, 7.0 }
        };
        // Create PCA instance
        var pca = new PCA<double>(data);

        // Transform data
        var transformedData = pca.Transform();

        // Print transformed data
        Console.WriteLine("Transformed Data:");
        for (int i = 0; i < transformedData.GetLength(0); i++)
        {
            for (int j = 0; j < transformedData.GetLength(1); j++)
            {
                Console.Write($"{transformedData[i, j]} ");
            }
            Console.WriteLine();
        }

        // Print explained variance ratios
        var explainedVarianceRatio = pca.GetExplainedVarianceRatio();
        Console.WriteLine("Explained Variance Ratio:");
        foreach (var ratio in explainedVarianceRatio)
        {
            Console.WriteLine(ratio);
        }
    }
}

Vorcyc.Mathematics.MachineLearning.Decomposition.ICA<TSelf> class

ICA<TSelf> performs Independent Component Analysis (FastICA) for blind source separation: it recovers statistically independent, non-Gaussian source signals from their linear mixtures (up to scale and permutation). The input is laid out like PCA: each row is a sample (observation over time) and each column is one observed mixed signal.

It supports three contrast (nonlinearity) functions and two estimation strategies:

  • ICANonlinearity: LogCosh (default, g(u) = tanh(alpha * u)), Exp (g(u) = u * exp(-u^2 / 2), robust to outliers / highly super-Gaussian sources), Cube (g(u) = u^3, fast but outlier-sensitive).
  • ICAAlgorithm: Symmetric (default, estimates all components in parallel with symmetric decorrelation), Deflation (estimates components one at a time with Gram-Schmidt deflation).

Properties

1. Task

  • public MachineLearningTask Task { get; }
    • Task type; reports MachineLearningTask.SourceSeparation.

2. NumberOfComponents

  • public int NumberOfComponents { get; }
    • The number of independent components extracted.

3. Nonlinearity

  • public ICANonlinearity Nonlinearity { get; }
    • The contrast function used during estimation.

4. Algorithm

  • public ICAAlgorithm Algorithm { get; }
    • The estimation strategy (symmetric or deflation) used.

5. HasConverged

  • public bool HasConverged { get; }
    • Whether the fixed-point iteration converged within the iteration limit.

6. Iterations

  • public int Iterations { get; }
    • The number of iterations that were actually performed.

7. Mean

  • public TSelf[] Mean { get; }
    • A copy of the per-feature mean that was removed during centering.

Constructors

1. ICA

  • public ICA(TSelf[,] data, int? numberOfComponents = null, ICANonlinearity nonlinearity = ICANonlinearity.LogCosh, ICAAlgorithm algorithm = ICAAlgorithm.Symmetric, int maxIterations = 200, double tolerance = 1e-4, double alpha = 1.0, int? randomSeed = null, ComputingContext? context = null)
    • Initializes the ICA instance and runs FastICA.
    • Parameters:
      • data: Input data (rows = samples, columns = observed signals).
      • numberOfComponents: Number of components to extract (defaults to the number of input signals).
      • nonlinearity: Contrast function (LogCosh / Exp / Cube).
      • algorithm: Estimation strategy (Symmetric / Deflation).
      • maxIterations: Maximum number of fixed-point iterations.
      • tolerance: Convergence tolerance on the change of the unmixing matrix.
      • alpha: Slope of the log-cosh contrast (typically in [1, 2]); ignored by other nonlinearities.
      • randomSeed: Optional seed for reproducible initialization.
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. Transform

  • public TSelf[,] Transform()
    • Returns the recovered source signals.
    • Returns: A [samples, components] matrix of estimated sources.

2. InverseTransform

  • public TSelf[,] InverseTransform(TSelf[,] sources)
    • Reconstructs the observed signals from sources using the mixing matrix.
    • Returns: A [samples, features] matrix of reconstructed observations.

3. GetMixingMatrix

  • public TSelf[,] GetMixingMatrix()
    • Gets the estimated mixing matrix A (observed = A * sources).
    • Returns: A [features, components] matrix.

4. GetUnmixingMatrix

  • public TSelf[,] GetUnmixingMatrix()
    • Gets the estimated unmixing matrix W (sources = W * centered-observed).
    • Returns: A [components, features] matrix.

Code example

The following example separates two mixed sources with FastICA:

using System;
using Vorcyc.Mathematics.MachineLearning.Decomposition;

public class ICAExample
{
    public static void Main()
    {
        int n = 2000;
        var data = new double[n, 2];
        for (int t = 0; t < n; t++)
        {
            double s1 = Math.Sin(t * 0.02);             // sine source
            double s2 = Math.Sign(Math.Sin(t * 0.047));  // square source

            // Linear mixing of the two independent sources.
            data[t, 0] = 0.60 * s1 + 0.40 * s2;
            data[t, 1] = 0.35 * s1 + 0.75 * s2;
        }

        // Use the exp nonlinearity with deflation (defaults are LogCosh + Symmetric).
        var ica = new ICA<double>(
            data,
            nonlinearity: ICANonlinearity.Exp,
            algorithm: ICAAlgorithm.Deflation,
            randomSeed: 7);

        var sources = ica.Transform(); // [n, 2] recovered independent sources

        Console.WriteLine($"Converged: {ica.HasConverged}, Iterations: {ica.Iterations}");
        Console.WriteLine($"Components: {ica.NumberOfComponents}");
    }
}

Vorcyc.Mathematics.MachineLearning.DimensionalityReduction.TSNE<T> class

TSNE<T> implements t-SNE for dimensionality reduction and visualization.

Properties

No public properties.

Constructors

1. TSNE

  • public TSNE(int perplexity = 30, int maxIter = 1000, T learningRate = default, Action<int, T>? progressCallback = null, ComputingContext? context = null)
    • Initializes TSNE class instance.
    • Parameters:
      • perplexity: Perplexity.
      • maxIter: Maximum iterations.
      • learningRate: Learning rate; defaults to 200.0 when default.
      • progressCallback: Optional progress callback invoked with (iteration, cost).
      • context: Optional execution policy; when null the ambient scope or default context is used.

Methods

1. FitTransform

  • public Matrix<T> FitTransform(Matrix<T> data)
    • Runs t-SNE and returns reduced matrix.
    • Parameters:
      • data: High-dimensional input matrix.
    • Returns: Reduced matrix.

Code example

The following example uses TSNE<T> with comments:

using System;
using Vorcyc.Mathematics.MachineLearning.DimensionalityReduction;
using Vorcyc.Mathematics.LinearAlgebra;
public class TSNEExample
{
    public static void Main()
    {
        // Data matrix
        var data = new Matrix<double>(
            new double[,]
            { 
                { 1.0, 2.0, 3.0 }, 
                { 2.0, 3.0, 4.0 }, 
                { 3.0, 4.0, 5.0 }, 
                { 4.0, 5.0, 6.0 }, 
                { 5.0, 6.0, 7.0 } 
            });
        // Create t-SNE instance
        var tsne = new TSNE<double>();

        // Run t-SNE
        var transformedData = tsne.FitTransform(data);

        // Print reduced data
        Console.WriteLine("Transformed Data:");
        for (int i = 0; i < transformedData.Rows; i++)
        {
            for (int j = 0; j < transformedData.Columns; j++)
            {
                Console.Write($"{transformedData[i, j]} ");
            }
            Console.WriteLine();
        }
    }
}

Distance metric classes

Vorcyc.Mathematics.MachineLearning.Distances.Angular<TSelf> class

Description

Angular distance; the proper metric version of cosine distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.
  • public static TSelf Similarity(TSelf[] x, TSelf[] y)

    • Gets the similarity between two points.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Similarity between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.ArgMax<TSelf> class

Description

ArgMax distance (L0 distance).

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.BrayCurtis<TSelf> class

Description

Bray–Curtis distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Canberra<TSelf> class

Description

Canberra distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Chebyshev<TSelf> class

Description

Chebyshev distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Cosine<TSelf> class

Description

Cosine distance. For a proper metric, see Angular distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.
  • public static TSelf Similarity(TSelf[] x, TSelf[] y)

    • Gets the similarity between two points.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Similarity between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Dice<TSelf> class

Description

Dice dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Euclidean<TSelf> class

Description

Euclidean distance metric.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.
  • public static TSelf Similarity(TSelf[] x, TSelf[] y)

    • Gets the similarity between two points.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Similarity between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Hamming<TSelf> class

Description

Hamming distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Hellinger<TSelf> class

Description

Hellinger distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Jaccard<TSelf> class

Description

Jaccard (index) distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.
  • public static TSelf Similarity(TSelf[] x, TSelf[] y)

    • Gets the similarity between two points.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Similarity between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Kulczynski<TSelf> class

Description

Kulczynski dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Levenshtein class

Description

Levenshtein distance.

Methods

  • public double Distance(string x, string y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Manhattan<TSelf> class

Description

Manhattan (taxicab or L1) distance.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Matching<TSelf> class

Description

Matching dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Minkowski<TNumber> class

Description

Minkowski distance generalizes Euclidean and Manhattan distance in a normed vector space.

Properties

  • public static TNumber Order { get; set; }
    • Gets or sets Minkowski order p.

Methods

  • public static TNumber Distance(TNumber[] x, TNumber[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.PearsonCorrelation<TSelf> class

Description

Pearson correlation similarity.

Methods

  • public static TSelf Similarity(TSelf[] x, TSelf[] y)
    • Computes the similarity between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Similarity between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.RogersTanimoto<TSelf> class

Description

Rogers–Tanimoto dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.RusselRao<TSelf> class

Description

Russel–Rao dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.SokalMichener<TSelf> class

Description

Sokal–Michener dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.SokalSneath<TSelf> class

Description

Sokal–Sneath dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)
    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.WeightedEuclidean<TSelf> class

Description

Weighted Euclidean distance and similarity. Returns Sqrt(Σ wᵢ·(xᵢ−yᵢ)²), the standard weighted Euclidean metric.

Properties

  • public static TSelf[] Weights { get; set; }
    • Gets or sets per-dimension weights (default all ones).

Methods

  • public static void SetDimensions(int dimensions)

    • Sets Weights to all ones for the given dimension count.
    • Parameters:
      • dimensions: Number of dimensions (columns).
  • public static TSelf Distance(TSelf[] x, TSelf[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.
  • public static TSelf Similarity(TSelf[] x, TSelf[] y)

    • Gets the similarity between two points.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Similarity between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.WeightedSquareEuclidean<TSelf> class

Description

Weighted squared Euclidean distance and similarity. Not a metric (violates triangle inequality).

Properties

  • public TSelf[] Weights { get; set; }
    • Gets or sets per-dimension weights (default all ones).

Methods

  • public static void SetDimensions(int dimensions)

    • Sets Weights to all ones for the given dimension count.
    • Parameters:
      • dimensions: Number of dimensions (columns).
  • public static TSelf Distance(TSelf[] x, TSelf[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.
  • public static TSelf Similarity(TSelf[] x, TSelf[] y)

    • Gets the similarity between two points.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Similarity between x and y.

Vorcyc.Mathematics.MachineLearning.Distances.Yule<TSelf> class

Description

Yule dissimilarity.

Methods

  • public static TSelf Distance(TSelf[] x, TSelf[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: Distance between x and y.
  • public static double Distance(int[] x, int[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: double distance between x and y.
  • public static float Distance(float[] x, float[] y)

    • Computes the distance between points x and y.
    • Parameters:
      • x: First point。
      • y: Second point。
    • Returns: float distance between x and y.

Language / 语言: English · 简体中文


Home


首页

Clone this wiki locally