Categories &

Functions List

Class Definition: ClassificationKNN

statistics: ClassificationKNN

K-nearest neighbors classification

The ClassificationKNN class implements a K-nearest neighbor classifier object, which can predict responses for new data using the predict method. The implemented algorithm allows you choose a range of different distance metrics, the number of nearest neighbors, as well as the searching algorithm.

The K-nearest neighbors (k-NN) classifier is a simple, non-parametric machine learning algorithm used for classification tasks. It classifies a data point based on the majority class of its k closest neighbors in the feature space.

Create a ClassificationKNN object by using the fitcknn function or the class constructor.

See also: fitcknn

Source Code: ClassificationKNN

The ClassificationKNN class contains the following properties:

A numeric column vector with one entry per observation used for fitting. Each class carries its prior spread evenly over its own observations, so an observation of class k weighs Prior(k) divided by the number of observations in that class. This property is read-only.

Each class carries its prior spread evenly over its own observations, so an observation of a class weighs Prior for that class divided by the number of observations it holds.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A numeric matrix containing the unstandardized predictor data. Each column of X represents one predictor (variable), and each row represents one observation. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

Specified as a logical or numeric column vector, or as a character array or a cell array of character vectors with the same number of rows as the predictor data. Each row in Y is the observed class label for the corresponding row in X. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A positive integer value specifying the number of observations in the training dataset used for training the ClassificationKNN model. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A logical column vector with the same length as the observations in the original predictor data X, true for each row that was used for fitting the ClassificationKNN model. It is empty, [], when every observation was used, so a non-empty value means that rows holding missing values were dropped. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A positive integer value specifying the number of predictors in the training dataset used for training the ClassificationKNN model. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A cell array of character vectors specifying the names of the predictor variables. The names are in the order in which they appear in the training dataset. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A numeric vector of column indices into X naming the predictors treated as categorical, and empty when none is. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A cell array of character vectors. It matches PredictorNames unless a categorical predictor was expanded into indicator variables. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A character vector specifying the name of the response variable Y. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

An array of unique values of the response variable Y, which has the same data types as the data in Y. This property is read-only. ClassNames can have any of the following datatypes:

  • Cell array of character vectors
  • Character array
  • Logical vector
  • Numeric vector

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A numeric vector of the same length as the columns in X with the standard deviations corresponding to each predictor. If the predictor variables have not been standardized, then 'obj.Sigma' is empty. This property is read-only.

Each predictor is summarized from every observation where that predictor is present, so a row holding a missing value in another predictor still contributes to this one.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A numeric vector of the same length as the columns in X with the mean values corresponding to each predictor. If the predictor variables have not been standardized, then 'obj.Mu' is empty. This property is read-only.

Each predictor is summarized from every observation where that predictor is present, so a row holding a missing value in another predictor still contributes to this one.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A character vector specified as either 'kdtree', which creates and uses a Kd-tree to find nearest neighbors, or 'exhaustive', which uses the exhaustive search algorithm by computing the distance values from all points in X to find nearest neighbors.

Change the NSMethod property using dot notation as in:

  • obj.NSMethod = newNSMethod

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A positive integer scalar specifying the maximum number of data points in the leaf node of the Kd-tree. BucketSize only applies when the NSMethod property is 'kdtree'.

Change the BucketSize property using dot notation as in:

  • obj.BucketSize = maxnum

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A cell array with one entry per predictor, holding that predictor’s bin edges where the learner discretized it before fitting. It is empty here and stays empty: this learner fits the predictors as they are, and MATLAB’s reports an empty cell for it as well.

This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A structure holding the parameters of the fit: NumNeighbors, NSMethod, Distance, BucketSize, IncludeTies, DistanceWeight, BreakTies, Exponent, Cov, Scale, StandardizeData, and the Version, Method and Type tags.

Each of the three distance parameters belongs to one metric and is empty under the others: Exponent to 'minkowski', Cov to 'mahalanobis' and Scale to 'seuclidean'. Cov and Scale hold what was passed and stay empty otherwise, while Exponent carries its default of 2 for a 'minkowski' fit that did not name one. What the fit used in every case is the DistParameter property. BucketSize is likewise empty unless the search is 'kdtree', the only method that reads it. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

Always empty. It is declared for MATLAB compatibility, where it holds what an automatic search over the hyperparameters found. This class fits the parameters it is given and runs no such search, so there is nothing to report. This property is read-only.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A positive integer value specifyingNumber of nearest neighbors in X used to classify each point during prediction. Change the NumNeighbors property using dot notation as in:

  • obj.NumNeighbors = newNumNeighbors

This property may be assigned after fitting. A value larger than NumObservations is reduced to it rather than refused.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A character vector specifying the distance metric used by the neighbor-searcher method, or a function handle to a custom distance function. See the available distance metrics in knnsearch for more info. A custom distance function must have the form D2 = distfun (ZI, ZJ), where ZI is a 1×N vector containing one row of the predictor data, ZJ is an M2×N matrix containing multiple rows of the predictor data, and D2 is an M2×1 vector of distances whose k-th element is the distance between the observations ZI and ZJ(k,:). A custom distance function carries no DistParameter. Change the Distance property using dot notation as in:

  • obj.Distance = newDistance

This property may be assigned after fitting. NSMethod is read-only and constrains it: a 'kdtree' model takes 'euclidean', 'cityblock', 'chebychev' and 'minkowski' only, and never a function handle. Assigning a different metric recomputes DistParameter, since a parameter belonging to one metric means nothing under another.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A character vector or a function handle specifying the distance weighting function, which can be any of the following values:

  • 'equal', which corresponds to @(d) d.
  • 'inverse', which corresponds to @(d) 1/d.
  • 'squaredinverse', which corresponds to @(d) 1/d.^2.
  • @fcn, which is a function handle that accepts a matrix of nonnegative distances, and returns a matrix the same size containing nonnegative distance weights.

Change the DistanceWeight property using dot notation as in:

  • obj.DistanceWeight = newDistanceWeight

A character vector naming the weight, or the func2str form of a supplied handle. This property may be assigned after fitting.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A character vector specifying the tie-breaking algorithm used by the predict method, when multiple classes have the same smallest cost. It can be one of the following:

  • 'smallest' (default), which favors the class with the smallest index among the tied groups, i.e. the one that appears first in the training labelled data.
  • 'nearest', which favors the class with the nearest neighbor among the tied groups, i.e. the class with the closest member point according to the distance metric used.
  • 'random', which randomly picks one class among the tied groups.

The tie-breaking algorithm is only used when IncludeTies is false. Change the BreakTies property using dot notation as in:

  • obj.BreakTies = algorithm

This property may be assigned after fitting. It decides the label when two classes hold the same weight among the neighbours, and it applies whether or not IncludeTies is set.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A logical scalar specifying whether prediction includes all the neighbors whose distance values are equal to the k^th smallest distance. If IncludeTies is true, prediction includes all of these neighbors. Otherwise, prediction uses exactly k neighbors.

Change the IncludeTies property using dot notation as in:

  • obj.IncludeTies = flag

This property may be assigned after fitting.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A positive definite covariance matrix, a positive scalar, or a vector of positive scale values specifying the parameter for the corresponding distance metric as shown below:

  • 'mahalanobis' accepts a positive definite covariance matrix.
  • 'minkowski' accepts a positive scalar as the Minkowski distance exponent.
  • 'seuclidean' accepts a vector of positive scale values of equal length as the number of predictors in X.

For any other distance metric, DistParameter is empty ([]). Change the DistParameter property using dot notation as in:

  • obj.DistParameter = distParam

This property may be assigned after fitting, but only under the three metrics that carry one: 'minkowski', 'seuclidean' and 'mahalanobis'. Under any other metric there is nothing for it to mean and the assignment is refused.

Deviation from MATLAB. A 'seuclidean' scale of zeros is refused here. MATLAB accepts it, then warns from inside its distance routine at predict time and answers anyway, which contradicts its own message that the scale must hold positive values. A zero scale divides that predictor by nothing, so it is rejected where it is given rather than surfacing later as a warning attached to an answer.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A square matrix specifying the cost of misclassification of a point. Cost(i,j) is the cost of classifying a point into class j if its true class is i (that is, the rows correspond to the true class and the columns correspond to the predicted class). The order of the rows and columns in Cost corresponds to the order of the classes in ClassNames. The number of rows and columns in Cost is the number of unique classes in the response. By default, Cost(i,j) = 1 if i != j, and Cost(i,j) = 0 if i = j. In other words, the cost is 0 for correct classification and 1 for incorrect classification.

Add or change the Cost property using dot notation as in:

  • obj.Cost = costMatrix

A cost may also be given as a struct with the fields ClassNames and ClassificationCosts, which names the order its own matrix is written in. That matrix is permuted into the order of ClassNames above, so a caller need not know which order the classes were sorted into. It must name every class.

A cost must be floating point, not sparse, not complex, non-negative and zero down its diagonal, and must hold no NaN or Inf. A single is widened to double.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A numeric vector specifying the prior probabilities for each class. The order of the elements in Prior corresponds to the order of the classes in ClassNames.

Add or change the Prior property using dot notation as in:

  • obj.Prior = priorVector

Specified as a row vector with one entry per class, in the order of ClassNames, and rescaled to sum to one. It may be given as 'empirical', 'uniform', a numeric vector, or a structure with ClassNames and ClassProbs fields, which assigns each probability by class name rather than by position.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

Specified as a function handle for transforming the classification scores. Add or change the ScoreTransform property using dot notation as in:

  • obj.ScoreTransform = 'function_name'
  • obj.ScoreTransform = @function_handle

When specified as a character vector, it can be any of the following built-in functions. Nevertheless, the ScoreTransform property always stores their function handle equivalent.

ValueDescription
'doublelogit'1 ./ (1 + exp (-2 × x))
'invlogit'log (x ./ (1 - x))
'ismax'Sets the score for the class with the largest score to 1, and for all other classes to 0
'logit'1 ./ (1 + exp (-x))
'none'x (no transformation)
'identity'x (no transformation)
'sign' -1 for x < 0, 0 for x = 0, 1 for x > 0
'symmetric'2 × x - 1
'symmetricismax'Sets the score for the class with the largest score to 1, and for all other classes to -1
'symmetriclogit'2 ./ (1 + exp (-x)) - 1

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

A positive scalar giving the cache size in megabytes, 1000 by default. Change the CacheSize property using dot notation as in:

  • obj.CacheSize = newCacheSize

This property is stored and reported for compatibility and does not affect the fit or any prediction. A nearest-neighbour model keeps no Gram matrix to cache: it holds the training data and computes each distance when asked. Assigning it changes nothing but the value read back.

MATLAB carries the same property and hides it from properties, where this package reports it, so that a value a user may set is a value a user can find.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

The ClassificationKNN class offers the following public methods:

statistics: obj = ClassificationKNN (X, Y)
statistics: obj = ClassificationKNN (…, name, value)

obj = ClassificationKNN (X, Y) returns a ClassificationKNN object, with X as the predictor data and Y containing the class labels of observations in X.

  • X must be a N×P numeric matrix of input data where rows correspond to observations and columns correspond to features or variables. X will be used to train the kNN model.
  • Y is N×1 matrix or cell matrix containing the class labels of corresponding predictor data in X. Y can contain any type of categorical data. Y must have same numbers of Rows as X.

obj = ClassificationKNN (…, name, value) returns a ClassificationKNN object with parameters specified by the following name, value paired input arguments:

NameValue
'PredictorNames'A cell array of character vectors specifying the names of the predictors. The length of this array must match the number of columns in X.
'ResponseName'A character vector specifying the name of the response variable.
'ClassNames'Names of the classes in the class labels, Y, used for fitting the GAM model. ClassNames are of the same type as the class labels in Y.
'Cost'An N×R numeric matrix containing misclassification cost for the corresponding instances in X, where R is the number of unique categories in Y. If an instance is correctly classified into its category the cost is calculated to be 1, otherwise 0. The cost matrix can be altered by using Mdl.cost = somecost. By default, its value is cost = ones (rows (X), numel (unique (Y))).
'Prior'A numeric vector specifying the prior probabilities for each class. The order of the elements in Prior corresponds to the order of the classes in ClassNames. Alternatively, you can specify 'empirical' to use the empirical class probabilities or 'uniform' to assume equal class probabilities.
'ScoreTransform'A user-defined function handle or a character vector specifying one of the following builtin functions specifying the transformation applied to predicted classification scores. Supported values include 'doublelogit', 'invlogit', 'ismax', 'logit', 'none', 'identity', 'sign', 'symmetric', 'symmetricismax', and 'symmetriclogit'.
'BreakTies'A character vector specifying the tie-breaking algorithm used by predict method, when multiple classes have the same smallest cost. Available options are 'smallest' (default), which uses the smallest index among tied groups, 'nearest', which uses the class with the nearest neighbor among tied groups, and 'random', which randomly selects one of the tied groups.
'NumNeighbors'A positive integer value that specifies the number of nearest neighbors to be found in the kNN search algorithm for classifying each point during prediction. By default, it is 1.
'Distance'Any valid distance metric supported by the pdist2 function. Note that the allowable distance metrics depend on the selected nearest neighbor search method.
'DistanceWeight'Either a distance weighting function, specified either as a function handle, which accepts a matrix of nonnegative distances and returns a matrix the same size containing nonnegative distance weights, or a character vector with one of the following values: 'equal', which corresponds to no weighting; 'inverse', which corresponds to a weight equal to 1/distance; 'squaredinverse', which corresponds to a weight equal to 1/distance^2.
'Cov'A square matrix with the same number of columns X specifying the covariance matrix for computing the mahalanobis distance. This must be a positive definite matrix matching. This argument is only valid when the selected distance metric is 'mahalanobis'.
'Exponent'A positive scalar (usually an integer) specifying the Minkowski distance exponent. This argument is only valid when the selected distance metric is 'minkowski'. By default, it is 2.
'Scale'A nonnegative numeric vector specifying the scale parameters for the standardized Euclidean distance. The vector length must be equal to the number of columns in X. This argument is only valid when the selected distance metric is 'seuclidean', in which case each coordinate of X is scaled by the corresponding element of 'scale', as is each query point in Y. By default, the scale parameter is the standard deviation of each coordinate in X. If a variable in X is constant, i.e. zero variance, this value is forced to 1 to avoid division by zero. This is the equivalent of this variable not being standardized.
'NSMethod'A character vector specifying the nearest neighbor search method used by knnsearch, which can be 'kdtree' or 'exhaustive'. See knnsearch for more information about default values and allowable distance metrics for each search method.
'BucketSize'A positive integer value specifying the maximum number of data points in the leaf node of the Kd-tree. This argument is meaningful only when the selected nearest neighbor search method is 'kdtree'. By default, it is 50.

See also: fitcknn, knnsearch, rangesearch, pdist2

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: labels = predict (obj, XC)
ClassificationKNN: [labels, scores, cost] = predict (obj, XC)

labels = predict (obj, XC) returns the matrix of labels predicted for the corresponding instances in XC, using the predictor data in obj.X and corresponding labels, obj.Y, stored in the k-Nearest Neighbor classification model, obj.

  • obj must be a ClassificationKNN class object.
  • XC must be an M×P numeric matrix with the same number of features P as the corresponding predictors of the SVM model in obj.

[labels, scores, cost] = predict (obj, XC) also returns scores, which contains the predicted class scores or posterior probabilities for each instance of the corresponding unique classes, and cost, which is a matrix containing the expected cost of the classifications. By default, scores returns the posterior probabilities for KNN models, unless a specific ScoreTransform function has been specified. See fitcknn for more info.

Note! predict is explicitly using 'exhaustive' as the nearest search method due to the very slow implementation of 'kdtree' in the knnsearch function.

See also: fitcknn, ClassificationKNN, knnsearch

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: L = loss (obj, X, Y)
ClassificationKNN: L = loss (…, name, value)

L = loss (obj, X, Y) computes the loss, L, using the default loss function 'mincost'.

  • obj is a ClassificationKNN object trained on X and Y.
  • X must be a N×P numeric matrix of input data where rows correspond to observations and columns correspond to features or variables.
  • Y is N×1 matrix or cell matrix containing the class labels of corresponding predictor data in X. Y must have same numbers of Rows as X.

L = loss (…, name, value) allows additional options specified by name-value pairs:

NameValue
'LossFun'Specifies the loss function to use. Can be a function handle with four input arguments (C, S, W, Cost) which returns a scalar value or one of: ’binodeviance’, ’classifcost’, ’classiferror’, ’exponential’, ’hinge’, ’logit’,’mincost’, ’quadratic’.
  • C is a logical matrix of size N×K, where N is the number of observations and K is the number of classes. The element C(i,j) is true if the class label of the i-th observation is equal to the j-th class.
  • S is a numeric matrix of size N×K, where each element represents the classification score for the corresponding class.
  • W is a numeric vector of length N, representing the observation weights.
  • Cost is a K×K matrix representing the misclassification costs.
'Weights'Specifies observation weights, must be a numeric vector of length equal to the number of rows in X. Default is ones (size (X, 1)). loss normalizes the weights so that observation weights in each class sum to the prior probability of that class. When you supply Weights, loss computes the weighted classification loss.

See also: fitcknn, ClassificationKNN

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: m = margin (obj, X, Y)

  • obj is a ClassificationKNN object trained on X and Y.
  • X must be a N×P numeric matrix of input data where rows correspond to observations and columns correspond to features or variables.
  • Y is N×1 matrix or cell matrix containing the class labels of corresponding predictor data in X. Y must have same numbers of Rows as X.

The classification margin for each observation is the difference between the classification score for the true class and the maximal classification score for the false classes.

See also: fitcknn, ClassificationKNN

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: [pd, x, y] = partialDependence (obj, Vars, Labels)
ClassificationKNN: [pd, x, y] = partialDependence (…, Data)
ClassificationKNN: [pd, x, y] = partialDependence (…, name, value)

[pd, x, y] = partialDependence (obj, Vars, Labels) computes the partial dependence of the classification scores on the variables Vars for the specified class Labels.

  • obj is a trained ClassificationKNN object.
  • Vars is a vector of positive integers, character vector, string array, or cell array of character vectors representing predictor variables (it can be indices of predictor variables in obj.X).
  • Labels is a character vector, logical vector, numeric vector, or cell array of character vectors representing class labels. (column vector)

[pd, x, y] = partialDependence (…, Data) specifies new predictor data to use for computing the partial dependence.

[pd, x, y] = partialDependence (…, name, value) allows additional options specified by name-value pairs:

NameValue
'NumObservationsToSample'Number of observations to sample. Must be a positive integer. Defaults to the number of observations in the training data.
'QueryPoints'Points at which to evaluate the partial dependence. Must be a numeric column vector, numeric two-column matrix, or cell array of character column vectors.
'UseParallel'Logical value indicating whether to perform computations in parallel. Defaults to false.

Return Values

  • pd: Partial dependence values.
  • x: Query points for the first predictor variable in Vars.
  • y: Query points for the second predictor variable in Vars (if applicable).

See also: fitcknn, ClassificationKNN

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: CVMdl = crossval (obj)
ClassificationKNN: CVMdl = crossval (…, Name, Value)

CVMdl = crossval (obj) returns a cross-validated model object, CVMdl, from a trained model, obj, using 10-fold cross-validation by default.

CVMdl = crossval (obj, name, value) specifies additional name-value pair arguments to customize the cross-validation process.

NameValue
'KFold'Specify the number of folds to use in k-fold cross-validation. "KFold", k, where k is an integer greater than 1.
'Holdout'Specify the fraction of the data to hold out for testing. "Holdout", p, where p is a scalar in the range (0,1).
'Leaveout'Specify whether to perform leave-one-out cross-validation. "Leaveout", Value, where Value is ’on’ or ’off’.
'CVPartition'Specify a cvpartition object used for cross-validation. "CVPartition", cv, where isa (cv, "cvpartition") = 1.

See also: fitcknn, ClassificationKNN, cvpartition, ClassificationPartitionedModel

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: e = edge (obj, X, Y)
ClassificationKNN: e = edge (…, "Weights", w)

e = edge (obj, X, Y) reduces the vector that margin returns to a single number, the mean margin over the rows of X. It says how far the model puts the true class ahead of its nearest rival on average, so a larger edge is a better model, and unlike a loss it is not bounded above and rewards confidence rather than bare correctness.

e = edge (…, "Weights", w) takes the weighted mean instead, with one weight per row of X.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: label = resubPredict (obj)
ClassificationKNN: [label, score, cost] = resubPredict (obj)

label = resubPredict (obj) is predict applied to the observations the model was fitted on, which it holds in X. Handing them over yourself is not the same thing: a row dropped for a missing response is not in X, so the original matrix and the model’s own are different data.

The result measures fit and not generalization, and is optimistic by construction. crossval is what estimates performance on data the model has not seen.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: m = resubMargin (obj)

m = resubMargin (obj) is margin applied to the observations the model was fitted on, one number per observation. Being a resubstitution quantity it is optimistic by construction.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: e = resubEdge (obj)

e = resubEdge (obj) is edge applied to the observations the model was fitted on, the mean of resubMargin.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: L = resubLoss (obj)
ClassificationKNN: L = resubLoss (…, name, value)

L = resubLoss (obj) is loss applied to the observations the model was fitted on, defaulting to 'mincost', and it accepts the same Name-Value pairs.

Being a resubstitution quantity it is a lower bound on the error rather than an estimate of it. It is worth least on a lazy learner: a one-neighbour ClassificationKNN has a resubstitution loss of exactly zero, every training point being its own nearest neighbour.

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)
ClassificationKNN: savemodel (obj, filename)

savemodel (obj, filename) saves each property of a ClassificationKNN object into an Octave binary file, the name of which is specified in filename, along with an extra variable, which defines the type classification object these variables constitute. Use loadmodel in order to load a classification object into Octave’s workspace.

See also: loadmodel, fitcknn, ClassificationKNN

Create a k-nearest neighbor classifier for Fisher's iris data with k = 5. Evaluate some model predictions on new data.

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)

Examples

 load fisheriris
 x = meas;
 y = species;
 xc = [min(x); mean(x); max(x)];
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);
 [label, score, cost] = predict (obj, xc)
label =
  3x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    

score =

   0.4000   0.6000        0
        0   1.0000        0
        0        0   1.0000

cost =

   0.6000   0.4000   1.0000
   1.0000        0   1.0000
   1.0000   1.0000        0
 load fisheriris
 x = meas;
 y = species;
 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Standardize', 1);

Create a cross-validated model

 CVMdl = crossval (obj)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 10
           ScoreTransform: 'none'
 load fisheriris
 x = meas;
 y = species;
 covMatrix = cov (x);

Fit the k-NN model using the 'mahalanobis' distance and the custom covariance matrix

 obj = fitcknn (x, y, 'NumNeighbors', 5, 'Distance','mahalanobis', ...
 'Cov', covMatrix);

Create a partition model using cvpartition

 Partition = cvpartition (size (x, 1), 'kfold', 12);

Create cross-validated model using 'cvPartition' name-value argument

 CVMdl = crossval (obj, 'cvPartition', Partition)
CVMdl =

  ClassificationPartitionedModel

      CrossValidatedModel: 'KNN'
             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
          NumObservations: 150
                    KFold: 12
           ScoreTransform: 'none'

Access the trained model from first fold of cross-validation

 CVMdl.Trained{1}
ans =

  ClassificationKNN

             ResponseName: 'Y'
               ClassNames: {'setosa' 'versicolor' 'virginica'}
           ScoreTransform: 'none'
          NumObservations: 137
            NumPredictors: 4
                 Distance: 'mahalanobis'
                 NSMethod: 'exhaustive'
             NumNeighbors: 5
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);
 customLossFun = @(C, S, W, Cost) sum (W .* sum (abs (C - S), 2));

Calculate loss using custom loss function

 L = loss (model, X, Y, 'LossFun', customLossFun)
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = {'A'; 'B'; 'A'};
 model = fitcknn (X, Y);

Calculate loss using 'mincost' loss function

 L = loss (model, X, Y, 'LossFun', 'mincost')
L = 0
 X = [1, 2; 3, 4; 5, 6];
 Y = ['1'; '2'; '3'];
 model = fitcknn (X, Y);
 X_test = [3, 3; 5, 7];
 Y_test = ['1'; '2'];

Specify custom Weights

 W = [1; 2];
 L = loss (model, X_test, Y_test, 'LossFun', 'logit', 'Weights', W);
 load fisheriris
 mdl = fitcknn (meas, species);
 X = mean (meas);
 Y = {'versicolor'};
 m = margin (mdl, X, Y)
m = 1
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 2;

Calculate partialDependence

 [pd, x, y] = partialDependence (mdl, Vars, Labels);
 X = [1, 2; 4, 5; 7, 8; 3, 2];
 Y = [2; 1; 3; 2];

Train the model

 mdl = fitcknn (X, Y);

Specify Vars and Labels

 Vars = 1;
 Labels = 1;
 queryPoints = [linspace(0, 1, 3)', linspace(0, 1, 3)'];

Calculate partialDependence using queryPoints

 [pd, x, y] = partialDependence (mdl, Vars, Labels, 'QueryPoints', ...
 queryPoints)
pd =

   0.2500   0.2500   0.2500

x =

        0        0
   0.5000   0.5000
   1.0000   1.0000

y = [](0x0)