KNeighborsClassifier<T>

Models & Types · AiDotNet.Classification.Neighbors

K-Nearest Neighbors classifier that predicts based on the majority class of nearest neighbors.

Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → KNeighborsClassifier<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

KNN is one of the simplest machine learning algorithms. Think of it as “you are the company you keep.” To classify something new:

  1. Find the k most similar training examples
  2. Look at their classes
  3. Return the most common class

Example: Classifying a fruit by its weight and color:

  • New fruit: 150g, red
  • 3 nearest neighbors: Apple (160g, red), Apple (145g, red), Cherry (10g, red)
  • Wait! Cherry is much smaller, so it’s not really “near”
  • Actual 3 nearest: Apple, Apple, Orange -> Predicted: Apple

This is why feature scaling is important for KNN!

How It Works

K-Nearest Neighbors (KNN) is a non-parametric, instance-based learning algorithm. It stores all training data and classifies new samples by finding the k closest training samples and voting on their class labels.

Example

var newSample = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
var labels = new Vector<double>(new double[] { 0.0, 1.0, 0.0, 1.0 });
var features = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
// Create a KNN classifier with k=5 neighbors
// Use AiModelBuilder facade for K-nearest-neighbors classification
var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new KNeighborsClassifier<double>(new KNeighborsOptions<double> { NNeighbors = 5 }));
            
var result = builder.Build(features, labels);
var prediction = result.Predict(newSample);

Constructors

Constructor Summary
KNeighborsClassifier(KNeighborsOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the KNeighborsClassifier class.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
Matrix<T> PredictProbabilities(Matrix<T> input)
ModelMetadata<T> GetModelMetadata()
Vector<T> ComputeGradients(Matrix<T> input, Vector<T> target, ILossFunction<T> lossFunction)
void ApplyGradients(Vector<T> gradients, T learningRate)
void Train(Matrix<T> x, Vector<T> y) Returns the model type identifier for this classifier.