FUNCTION REFERENCE

fitcknn

Fit a nearest-neighbour classification model.

Fit a nearest-neighbour classification model.

Syntax

mdl = fitcknn(X,Y)

Syntax

mdl = fitcknn(tbl,'ResponseVarName')

Syntax

mdl = fitcknn(tbl,Y)

Syntax

mdl = fitcknn(X,Y,'NumNeighbors',k,'Distance',metric,'Standardize',true)

Distances are euclidean (default), cityblock, chebychev and minkowski (exponent 2). Scores are neighbour class fractions in sorted ClassNames order; ties choose the first class. Labels preserve their numeric, logical, cellstr, string or categorical type. Common properties are ClassNames, ResponseName, PredictorNames, NumObservations, X and Y. Numeric table predictor columns are supported; prediction tables select training predictor names in training order.

Example

mdl=fitcknn([0;2],[1;2]); [label,score]=predict(mdl,1)

Differences from MATLAB: advanced distance weights and custom metrics are unsupported. Training is bounded to 20000 rows, 512 predictors, 2000000 elements and 256 classes; prediction comparison work is bounded, so large queries should be batched.

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.