Skip to content

ml_KNearest

mlclassOpenCV 5.0.0
import { ml_KNearest } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

Native object: release it with using or delete(). Factories can return null; check before calling methods. Inherits ml_StatModel.


K-Nearest Neighbour Classifier * *************************************************************************************** The class implements K-Nearest Neighbors model

See: ml_intro_knn

Constructors and members

static create

Creates the empty model

The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.
create(): ml_KNearest | null;
Returns

The ml_KNearest | null result.

static load

Loads and creates a serialized knearest from a file

Use KNearest::save to serialize and store an KNearest to disk. Load the KNearest from this file again, by calling this function with the path to the file.

load(filepath: EmbindString): ml_KNearest | null;
filepath

path to serialized KNearest

Returns

The ml_KNearest | null result.

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

getDefaultK

Default number of neighbors to use in predict method. See: setDefaultK

getDefaultK(): number;
Returns

The number result.

setDefaultK

Default number of neighbors to use in predict method. See: setDefaultK See: getDefaultK

setDefaultK(val: number): void;
val

val argument (number).

getIsClassifier

Whether classification or regression model should be trained. See: setIsClassifier

getIsClassifier(): boolean;
Returns

The boolean result.

setIsClassifier

Whether classification or regression model should be trained. See: setIsClassifier See: getIsClassifier

setIsClassifier(val: boolean): void;
val

val argument (boolean).

getEmax

Parameter for KDTree implementation. See: setEmax

getEmax(): number;
Returns

The number result.

setEmax

Parameter for KDTree implementation. See: setEmax See: getEmax

setEmax(val: number): void;
val

val argument (number).

getAlgorithmType

%Algorithm type, one of KNearest::Types. See: setAlgorithmType

getAlgorithmType(): number;
Returns

The number result.

setAlgorithmType

%Algorithm type, one of KNearest::Types. See: setAlgorithmType See: getAlgorithmType

setAlgorithmType(val: number): void;
val

val argument (number).

findNearest

Finds the neighbors and predicts responses for input vectors.

findNearest(samples: Mat, k: number, results: Mat, neighborResponses: Mat, dist: Mat): number;
3 available overloads
findNearest(samples: Mat, k: number, results: Mat): number;
findNearest(samples: Mat, k: number, results: Mat, neighborResponses: Mat): number;
findNearest(samples: Mat, k: number, results: Mat, neighborResponses: Mat, dist: Mat): number;
samples

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

k

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

results

Output destination, filled by the native operation. 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

Output destination, filled by the native operation. Optional output values for corresponding neighbors. It is a single- precision floating-point matrix of <number_of_samples> * k size.

dist

Output destination, filled by the native operation. 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.

For each input vector (a row of the matrix samples), the method finds the k nearest neighbors. In case of regression, the predicted result is a mean value of the particular vector's neighbor responses. In case of classification, the class is determined by voting.

For each input vector, the neighbors are sorted by their distances to the vector.

In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself.

If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method.

The function is parallelized with the TBB library.

Returns

The number result.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.