neighbors.KNeighborsClassifier

Classifier implementing the k-nearest neighbors vote.

Usage

const knn = new KNeighborsClassifier();
const X = [[0, 0, 0], [0, 1, 1], [1, 1, 0], [2, 2, 2], [1, 2, 2], [2, 1, 2]];
const y = [0, 0, 0, 1, 1, 1];
knn.fit(X ,y);
console.log(knn.predict([1, 2])); // predicts 1

Constructors

Methods

Constructors


constructor

new KNeighborsClassifier(__namedParameters: `object`)

Defined in neighbors/classification.ts:27

Parameters:

ParamTypeDefaultDescription
options.distancestringDISTEUCChoice of distance function, should choose between euclidean
options.knumber0Number of neighbors to classify
options.typestringTYPEKDType of algorithm to use, choose between kdtree(default)

Returns: KNeighborsClassifier

Methods


λ fit

Train the classifier with input and output data

Defined in neighbors/classification.ts:76

Parameters:

ParamTypeDefaultDescription
Xnumber[][] or string[][] or boolean[][]Training data.
ynumber[] or string[] or boolean[]Target data.

Returns:

void

λ fromJSON

Restores the model from a JSON checkpoint

Defined in neighbors/classification.ts:129

Parameters:

ParamTypeDefaultDescription
options.classesanynull
options.distanceanynull
options.kanynull
options.treeanynull
options.typeanynull

Returns:

void

λ predict

Predict single value from a list of data

Defined in neighbors/classification.ts:153

Parameters:

ParamTypeDefaultDescription
XT[][] or T[]Prediction data.

Returns:

any

λ toJSON

Return the model's state as a JSON object

Defined in neighbors/classification.ts:105

Returns:

ParamTypeDescription
classesany[]undefined
distanceanyundefined
knumberundefined
treeanyundefined
typestringundefined