MLkNNClassifier<T>
Models & Types · AiDotNet.Classification.MultiLabel
Implements ML-kNN (Multi-Label k-Nearest Neighbors) for multi-label classification.
Inheritance: MultiLabelClassifierBase<T> → MLkNNClassifier<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
ML-kNN extends k-NN to multi-label problems using Bayesian inference. For each label, it estimates the probability that a sample has the label given how many of its k nearest neighbors have that label.
How It Works
How it works:
- Find k nearest neighbors of the query sample
- Count how many neighbors have each label
- Use Bayesian inference with prior probabilities from training data
- Predict label if P(label=1|neighbors) > P(label=0|neighbors)
Key formula: P(H_l | E_l) = P(E_l | H_l) * P(H_l) / P(E_l) where H_l = label l is present, E_l = count of neighbors with label l
Reference: Zhang & Zhou, “ML-KNN: A lazy learning approach to multi-label learning” (2007)
Example
// Create ML-kNN for multi-label classification with Bayesian inference
var options = new MLkNNOptions<double>();
// Prepare features and multi-label targets
var features = new Matrix<double>(4, 2);
features[0, 0] = 1.0; features[0, 1] = 2.0;
features[1, 0] = 3.0; features[1, 1] = 4.0;
features[2, 0] = 5.0; features[2, 1] = 6.0;
features[3, 0] = 7.0; features[3, 1] = 8.0;
var labels = new Matrix<double>(4, 3);
labels[0, 0] = 1; labels[0, 1] = 0; labels[0, 2] = 1;
labels[1, 0] = 1; labels[1, 1] = 1; labels[1, 2] = 0;
labels[2, 0] = 0; labels[2, 1] = 1; labels[2, 2] = 1;
labels[3, 0] = 0; labels[3, 1] = 0; labels[3, 2] = 1;
// Train by storing instances and computing Bayesian priors
var result = new AiModelBuilder<double, Matrix<double>, Matrix<double>>()
.ConfigureModel(new MLkNNClassifier<double>(options))
.Build(features, labels);
// Predict labels using k-NN neighbor counts and Bayesian inference
var newSample = new Matrix<double>(1, 2);
newSample[0, 0] = 2.0; newSample[0, 1] = 3.0;
var prediction = result.Predict(newSample);
Constructors
| Constructor | Summary |
|---|---|
MLkNNClassifier(MLkNNOptions<T> options) |
Creates a new ML-kNN classifier. |
Methods
| Method | Summary |
|---|---|
Matrix<T> PredictMultiLabelProbabilities(Matrix<T> features) |
|
ModelOptions GetOptions() |