knnClassify

Purpose

Creates nearest neighbor predictions.

Format

y_hat = knnClassify(mdl, X)
Parameters:
  • mdl (struct) – A knnModel structure returned from a call to knnFit().
  • X (NxP matrix, or string array.) – The training data.
Returns:

y_hat (Nx1 vector, or string array.) – The predicted classes.

Examples

new;
library gml;

// Get file name with full path
fname = getGAUSSHome() $+ "pkgs/gml/examples/iris.csv";

// Load numeric predictors
X = loadd(fname, ". -Species");

// Load string labels
species = loaddSA(fname, "Species");

// Set seed for repeatable train/test sampling
rndseed 423432;

// Split data into (70%) train and (30%) test sets
{ y_train, y_test, X_train, X_test } = trainTestSplit(species, X, 0.7);

/*
** Train the model
*/

k = 3;

struct knnModel mdl;
mdl = knnFit(y_train, X_train, k);

/*
** Predictions on the test set
*/

y_hat = knnClassify(mdl, X_test);

print "First three predictions = " y_hat[1:3];
print "";
print "prediction accuracy = " meanc(y_hat .$== y_test);

The above code will print the following output:

First three predictions =
                virginica
               versicolor
                   setosa

prediction accuracy = 0.956

See also

knnFit()