Table of Contents

Class KNearest

Namespace
OpenCvSharp.ML
Assembly
OpenCvSharp.dll

K nearest neighbors classifier

public class KNearest : StatModel, IDisposable, ICvPtrHolder
Inheritance
KNearest
Implements
Inherited Members

Constructors

KNearest(nint)

Creates instance by raw pointer cv::ml::KNearest*

protected KNearest(nint p)

Parameters

p nint

Properties

AlgorithmType

Algorithm type, one of KNearest::Types.

public KNearest.Types AlgorithmType { get; set; }

Property Value

KNearest.Types

DefaultK

Default number of neighbors to use in predict method.

public int DefaultK { get; set; }

Property Value

int

Emax

Parameter for KDTree implementation

public int Emax { get; set; }

Property Value

int

IsClassifier

Whether classification or regression model should be trained.

public bool IsClassifier { get; set; }

Property Value

bool

Methods

Create()

Creates the empty model

public static KNearest Create()

Returns

KNearest

DisposeManaged()

Releases managed resources

protected override void DisposeManaged()

FindNearest(InputArray, int, OutputArray, OutputArray?, OutputArray?)

Finds the neighbors and predicts responses for input vectors.

public float FindNearest(InputArray samples, int k, OutputArray results, OutputArray? neighborResponses = null, OutputArray? dist = null)

Parameters

samples InputArray

Input samples stored by rows. It is a single-precision floating-point matrix of [number_of_samples] * k size.

k int

Number of used nearest neighbors. Should be greater than 1.

results OutputArray

Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with [number_of_samples] elements.

neighborResponses OutputArray

neighborResponses Optional output values for corresponding neighbors. It is a single-precision floating-point matrix of [number_of_samples] * k size.

dist OutputArray

Optional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of [number_of_samples] * k size.

Returns

float

Load(string)

Loads and creates a serialized model from a file.

public static KNearest Load(string filePath)

Parameters

filePath string

Returns

KNearest

LoadFromString(string)

Loads algorithm from a String.

public static KNearest LoadFromString(string strModel)

Parameters

strModel string

he string variable containing the model you want to load.

Returns

KNearest