Module_MachineLearning - vorcyc/Vorcyc.Mathematics GitHub Wiki
Location: Home / Machine learning
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) |
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);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 -- regressionData 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/LoadIsolationForestGaussian 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
CurveFittermethods (Linear / Polynomial / Exponential / Logarithmic / Power / Sinusoidal / CubicSpline / LOWESS / MovingAverage / Nonlinear / BayesianLinear) take optionalCancellationToken. GPR already did. Cubic splinePredictis binary search.
0.10.16:
CurveFitterPolynomial / Exponential / Logarithmic / PowerFit_SIMDpaths hoiststackallocout of hot loops, avoiding large-N (~≥5×10⁵)StackOverflowunder 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 featuresMost 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).
AdaBoostClassifierandSupportVectorMachineacceptcontextand exposeContextfor 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 instanceContextgovernsPredictBatch; thecontextargument ofModelBatchExtensions.PredictBatchonly applies to the generic fallback used by other classifiers.
The types below live in the
Vorcyc.Mathematics.MachineLearningnamespace and its sub-namespaces (.Regression,.Classfication,.Clustering,.DimensionalityReduction,.Decomposition,.Distances,.Preprocessing,.Serialization,.CurveFitting).
📒 Contents
- 🔖 0.9 classifier walkthrough
- 🔖 ComputingContext integration (parallel execution)
- 🔖 Vorcyc.Mathematics.MachineLearning.KNN<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.MultipleLinearRegression<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.SimpleLinearRegression<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.SupportVectorMachine<TSelf> class
- 🔖 Vorcyc.Mathematics.MachineLearning.Clustering.DBSCAN<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.Clustering.ExpectationMaximization<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.Clustering.GMM<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.Clustering.HierarchicalClustering<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.Clustering.KMeansClusterer<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.Clustering.VectorQuantization<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.DimensionalityReduction.FactorAnalysis<T> class
- 🔖 Vorcyc.Mathematics.MachineLearning.DimensionalityReduction.PCA<TSelf> class
- 🔖 Vorcyc.Mathematics.MachineLearning.Decomposition.ICA<TSelf> class
- 🔖 Vorcyc.Mathematics.MachineLearning.DimensionalityReduction.TSNE<T> class
- 🔖 Distance metric classes
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>.
-
public MachineLearningTask Task { get; }- Fixed to
MachineLearningTask.Regression。
- Fixed to
-
public KNN(ComputingContext? context = null)- Initializes KNN class instance.
- Parameters:
-
context: Optional execution policy; when null the ambient scope or default context is used.
-
-
public void Add(Point<T> point, T target)/Add(ReadOnlySpan<T> features, T target)- Adds a regression sample.
-
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.
-
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.
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);MultipleLinearRegression<T> fits multivariate linear regression. The normal equations use Cholesky (LinearEquationSolver.CholeskySolve) when positive definite, otherwise LU.
-
public IReadOnlyList<T> Coefficients { get; }- Gets regression coefficients.
-
public T Intercept { get; }- Gets regression intercept.
-
public T RSquared { get; }- Gets the coefficient of determination R².
-
public ComputingContext? Context { get; set; }- Execution policy honored by batch prediction; when null the ambient scope or default context is used.
-
public MachineLearningTask Task { get; }- Task type; supports regression.
-
public void Fit(T[,] x, T[] y)- Fits coefficients and intercept.
- Parameters:
-
x: Independent variable matrix. -
y: Dependent variable vector.
-
-
public T Predict(T[] x)- Predicts the dependent variable from inputs and coefficients.
- Parameters:
-
x: Independent variables.
-
- Returns: Predicted dependent value.
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);
}
}SimpleLinearRegression<T> performs simple (univariate) linear regression.
-
public T Slope { get; }- Gets linear regression slope.
-
public T Intercept { get; }- Gets linear regression intercept.
-
public T RSquared { get; }- Gets the coefficient of determination R².
-
public ComputingContext? Context { get; set; }- Execution policy honored by
PredictBatch; when null the ambient scope or default context is used.
- Execution policy honored by
-
public MachineLearningTask Task { get; }- Task type; supports regression.
-
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.
-
public T GetX(T y)- Computes x from y, slope, and intercept.
- Parameters:
-
y: Dependent variable value.
-
- Returns: Computed x.
-
public T GetY(T x)- Computes y from x, slope, and intercept.
- Parameters:
-
x: Independent variable value.
-
- Returns: Computed y.
-
public T[] PredictBatch(Span<T> x, ComputingContext? context = null)- Predicts y for a batch of x values; honors
Contextfor SIMD/parallel execution. - Parameters:
-
x: Independent variable values to evaluate. -
context: Optional execution policy overridingContext.
-
- Returns: Predicted y values, one per element of
x.
- Predicts y for a batch of x values; honors
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}");
}
}SupportVectorMachine<TSelf> is a simple linear SVM with selectable kernels.
-
public MachineLearningTask Task { get; }- Task type; supports classification and regression.
-
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.
-
-
public void Train(TSelf[][] inputs, int[] outputs)- Trains SVM on provided data.
- Parameters:
-
inputs: Training inputs (feature arrays). -
outputs: Output labels.
-
-
public int Predict(Span<TSelf> input)- Predicts class label for input.
- Parameters:
-
input: Feature array.
-
- Returns: Predicted label (1 or -1).
-
private TSelf PredictRaw(TSelf[] input)- Raw prediction score.
- Parameters:
-
input: Feature array.
-
- Returns: Raw prediction score.
-
private Func<TSelf[], TSelf[], TSelf> GetKernelFunction(SupportVectorMachineKernelType kernelType)- Gets the kernel function for the specified type.
- Parameters:
-
kernelType: Kernel type。
-
- Returns: Kernel delegate.
-
private static TSelf LinearKernel(TSelf[] x, TSelf[] y)- Linear kernel function.
- Parameters:
-
x: First input vector。 -
y: Second input vector。
-
- Returns: Kernel result.
-
private static TSelf PolynomialKernel(TSelf[] x, TSelf[] y)- Polynomial kernel function.
- Parameters:
-
x: First input vector。 -
y: Second input vector。
-
- Returns: Kernel result.
-
private static TSelf GaussianKernel(TSelf[] x, TSelf[] y)- Gaussian (RBF) kernel function.
- Parameters:
-
x: First input vector。 -
y: Second input vector。
-
- Returns: Kernel result.
-
private static TSelf SigmoidKernel(TSelf[] x, TSelf[] y)- Sigmoid kernel function.
- Parameters:
-
x: First input vector。 -
y: Second input vector。
-
- Returns: Kernel result.
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}");
}
}DBSCAN<T> performs DBSCAN clustering on 2D points.
-
public MachineLearningTask Task { get; }- Task type; supports clustering.
-
public DBSCAN(Point<T>[] points, T eps, int minPts, ComputingContext? context = null)- Initializes
DBSCANwith 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.
-
- Initializes
-
public List<List<Point<T>>> Cluster()- Runs DBSCAN clustering.
- Returns: List of clusters (each a list of points).
-
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.
-
-
private List<Point<T>> GetNeighbors(Point<T> point)- Gets neighbors of a point.
- Parameters:
-
point: Current point.
-
- Returns: List of neighbors.
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})");
}
}
}
}ExpectationMaximization<T> implements the expectation-maximization (EM) algorithm.
-
public IReadOnlyList<T[]> Means { get; }- Gets cluster centers (means).
-
public IReadOnlyList<Matrix<T>> Covariances { get; }- Gets covariance matrices.
-
public IReadOnlyList<T> Weights { get; }- Gets weights.
-
public MachineLearningTask Task { get; }- Task type; supports clustering.
-
public ExpectationMaximization(int numClusters)- Initializes ExpectationMaximization class instance。
- Parameters:
-
numClusters: Number of clusters.
-
-
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 whendefault.
-
-
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.
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);
}
}
}GMM<T> implements a Gaussian mixture model.
-
public IReadOnlyList<T[]> Means { get; }- Gets cluster centers (means).
-
public IReadOnlyList<Matrix<T>> Covariances { get; }- Gets covariance matrices.
-
public IReadOnlyList<T> Weights { get; }- Gets weights.
-
public MachineLearningTask Task { get; }- Task type; supports clustering.
-
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 whendefault. -
context: Optional execution policy; when null the ambient scope or default context is used.
-
-
public void Fit(List<T[]> data)- Fits data with GMM.
- Parameters:
-
data: Data as a list of arrays.
-
-
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.
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);
}
}
}HierarchicalClustering<T> performs hierarchical clustering on 2D points.
-
public MachineLearningTask Task { get; }- Task type; supports clustering.
-
public HierarchicalClustering(Point<T>[] points, ComputingContext? context = null)- Initializes
HierarchicalClusteringwith points. - Parameters:
-
points: Points to cluster. -
context: Optional execution policy; when null the ambient scope or default context is used.
-
- Initializes
-
public List<List<Point<T>>> Cluster(int k)- Hierarchical clustering until
kclusters remain. - Parameters:
-
k: Desired number of clusters.
-
- Returns: List of clusters (each a list of points).
- Hierarchical clustering until
-
private (int cluster1, int cluster2, T minDistance) FindClosestClusters()- Finds the two closest clusters.
- Returns: Indices of closest clusters and distance.
-
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.
-
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.
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})");
}
}
}
}KMeansClusterer<T> implements K-means clustering.
-
public MachineLearningTask Task { get; }- Task type; supports clustering.
-
public KMeansClusterer(T[][] data, int numClusters, int maxIterations, T tolerance, ComputingContext? context = null)- Initializes
KMeansClustererwith 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.
-
- Initializes
-
public int[] Cluster()- Runs K-means clustering.
- Returns: Cluster assignment per point.
-
private int[] InitClustering(int numData, int numClusters)- Initializes cluster assignments.
- Parameters:
-
numData: Number of data points. -
numClusters: Number of clusters.
-
- Returns: Initial cluster assignments.
-
private T[][] Allocate(int numRows, int numCols)- Allocates a 2D array.
- Parameters:
-
numRows: Row count. -
numCols: Column count.
-
- Returns: Allocated 2D array.
-
private bool UpdateCentroids()- Updates centroids.
- Returns: Whether centroids changed.
-
private bool UpdateClustering()- Updates cluster assignments.
- Returns: Whether assignments changed.
-
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.
-
private int MinIndex(T[] distances)- Index of minimum value in an array.
- Parameters:
-
distances: Distance array.
-
- Returns: Index of minimum.
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}");
}
}
}VectorQuantization<T> implements vector quantization.
-
public IReadOnlyList<T[]> Codebook { get; }- Gets codebook.
-
public MachineLearningTask Task { get; }- Task type; supports clustering.
-
public VectorQuantization(int codebookSize, int dimensions)- Initializes VectorQuantization class instance。
- Parameters:
-
codebookSize: Codebook size. -
dimensions: Vector dimensions.
-
-
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.
-
public T[] Predict(T[] vector)- Finds nearest code vector to input.
- Parameters:
-
vector: Input vector.
-
- Returns: Nearest code vector.
-
public void SaveCodebook(string filePath)- Saves codebook to file.
- Parameters:
-
filePath: File path.
-
-
public void LoadCodebook(string filePath)- Loads codebook from file.
- Parameters:
-
filePath: File path.
-
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");
}
}FactorAnalysis<T> performs factor analysis.
-
public Matrix<T> Loadings { get; private set; }- Gets factor loadings matrix.
-
public T[] Communalities { get; private set; }- Gets communalities array.
-
public T[] SpecificVariances { get; private set; }- Gets specific variances array.
-
public MachineLearningTask Task { get; }- Task type; supports dimensionality reduction.
-
public FactorAnalysis(ComputingContext? context = null)- Initializes FactorAnalysis class instance.
- Parameters:
-
context: Optional execution policy; when null the ambient scope or default context is used.
-
-
public void Analyze(Matrix<T> data, int numFactors)- Runs factor analysis.
- Parameters:
-
data: Data matrix (rows = samples, columns = variables). -
numFactors: Number of factors.
-
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);
}
}
}PCA<TSelf> performs principal component analysis for dimensionality reduction and feature extraction.
-
public MachineLearningTask Task { get; }- Task type; supports dimensionality reduction.
-
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
jis componentj.
- Principal axes; index
-
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.
-
-
public TSelf[,] Transform()- Transforms the (already centered) training data to principal components.
- Returns: Transformed principal components.
-
public TSelf[,] Transform(TSelf[,] x, int? components = null)- Centers
xwith the fitted means and projects onto the firstcomponentsaxes (all of them when null).
- Centers
-
public TSelf[] GetExplainedVarianceRatio()- Gets explained variance ratios.
- Returns: Explained variance ratio array.
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);
}
}
}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).
-
public MachineLearningTask Task { get; }- Task type; reports
MachineLearningTask.SourceSeparation.
- Task type; reports
-
public int NumberOfComponents { get; }- The number of independent components extracted.
-
public ICANonlinearity Nonlinearity { get; }- The contrast function used during estimation.
-
public ICAAlgorithm Algorithm { get; }- The estimation strategy (symmetric or deflation) used.
-
public bool HasConverged { get; }- Whether the fixed-point iteration converged within the iteration limit.
-
public int Iterations { get; }- The number of iterations that were actually performed.
-
public TSelf[] Mean { get; }- A copy of the per-feature mean that was removed during centering.
-
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.
-
-
public TSelf[,] Transform()- Returns the recovered source signals.
- Returns: A
[samples, components]matrix of estimated sources.
-
public TSelf[,] InverseTransform(TSelf[,] sources)- Reconstructs the observed signals from sources using the mixing matrix.
- Returns: A
[samples, features]matrix of reconstructed observations.
-
public TSelf[,] GetMixingMatrix()- Gets the estimated mixing matrix
A(observed = A * sources). - Returns: A
[features, components]matrix.
- Gets the estimated mixing matrix
-
public TSelf[,] GetUnmixingMatrix()- Gets the estimated unmixing matrix
W(sources = W * centered-observed). - Returns: A
[components, features]matrix.
- Gets the estimated unmixing matrix
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}");
}
}TSNE<T> implements t-SNE for dimensionality reduction and visualization.
No public properties.
-
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 to200.0whendefault. -
progressCallback: Optional progress callback invoked with(iteration, cost). -
context: Optional execution policy; when null the ambient scope or default context is used.
-
-
public Matrix<T> FitTransform(Matrix<T> data)- Runs t-SNE and returns reduced matrix.
- Parameters:
-
data: High-dimensional input matrix.
-
- Returns: Reduced matrix.
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();
}
}
}Angular distance; the proper metric version of cosine distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
-
public static TSelf Similarity(TSelf[] x, TSelf[] y)- Gets the similarity between two points.
- Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Similarity between
xandy.
ArgMax distance (L0 distance).
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Bray–Curtis distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Canberra distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Chebyshev distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Cosine distance. For a proper metric, see Angular distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
-
public static TSelf Similarity(TSelf[] x, TSelf[] y)- Gets the similarity between two points.
- Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Similarity between
xandy.
Dice dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Euclidean distance metric.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
-
public static TSelf Similarity(TSelf[] x, TSelf[] y)- Gets the similarity between two points.
- Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Similarity between
xandy.
Hamming distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Hellinger distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Jaccard (index) distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
-
public static TSelf Similarity(TSelf[] x, TSelf[] y)- Gets the similarity between two points.
- Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Similarity between
xandy.
Kulczynski dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Levenshtein distance.
-
public double Distance(string x, string y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Manhattan (taxicab or L1) distance.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Matching dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Minkowski distance generalizes Euclidean and Manhattan distance in a normed vector space.
-
public static TNumber Order { get; set; }- Gets or sets Minkowski order
p.
- Gets or sets Minkowski order
-
public static TNumber Distance(TNumber[] x, TNumber[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Pearson correlation similarity.
-
public static TSelf Similarity(TSelf[] x, TSelf[] y)- Computes the similarity between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Similarity between
xandy.
- Computes the similarity between points
Rogers–Tanimoto dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Russel–Rao dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Sokal–Michener dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Sokal–Sneath dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
Weighted Euclidean distance and similarity. Returns Sqrt(Σ wᵢ·(xᵢ−yᵢ)²), the standard weighted Euclidean metric.
-
public static TSelf[] Weights { get; set; }- Gets or sets per-dimension weights (default all ones).
-
public static void SetDimensions(int dimensions)- Sets
Weightsto all ones for the given dimension count. - Parameters:
-
dimensions: Number of dimensions (columns).
-
- Sets
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
-
public static TSelf Similarity(TSelf[] x, TSelf[] y)- Gets the similarity between two points.
- Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Similarity between
xandy.
Weighted squared Euclidean distance and similarity. Not a metric (violates triangle inequality).
-
public TSelf[] Weights { get; set; }- Gets or sets per-dimension weights (default all ones).
-
public static void SetDimensions(int dimensions)- Sets
Weightsto all ones for the given dimension count. - Parameters:
-
dimensions: Number of dimensions (columns).
-
- Sets
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
-
public static TSelf Similarity(TSelf[] x, TSelf[] y)- Gets the similarity between two points.
- Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Similarity between
xandy.
Yule dissimilarity.
-
public static TSelf Distance(TSelf[] x, TSelf[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns: Distance between
xandy.
- Computes the distance between points
-
public static double Distance(int[] x, int[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns:
doubledistance betweenxandy.
- Computes the distance between points
-
public static float Distance(float[] x, float[] y)- Computes the distance between points
xandy. - Parameters:
-
x: First point。 -
y: Second point。
-
- Returns:
floatdistance betweenxandy.
- Computes the distance between points