Categories &

Functions List

Function Reference: fitckernel

statistics: Mdl = fitckernel (X, Y)

statistics: Mdl = fitckernel (Tbl, ResponseVarName)

statistics: Mdl = fitckernel (Tbl, formula)

statistics: Mdl = fitckernel (Tbl, Y)

statistics: Mdl = fitckernel (…, name, value)

statistics: [Mdl, FitInfo] = fitckernel (…)

Fit a Gaussian kernel binary classifier.

Mdl = fitckernel (X, Y) returns a ClassificationKernel object fitted to the predictor data X and the two class response Y, where X is an NxP numeric matrix and Y has as many rows as X.

Mdl = fitckernel (…, name, value) passes the given Name-Value pairs to the model. They are documented under ClassificationKernel, and the ones most often wanted are 'Learner', 'NumExpansionDimensions', 'KernelScale', 'Lambda', 'BoxConstraint' and 'Standardize'.

[Mdl, FitInfo] = fitckernel (…) also returns a structure describing the optimization: the objective it reached, the gradient it left, and the tolerances it was given.

Mdl = fitckernel (…, cvopt, value) returns a ClassificationPartitionedKernel instead when one of 'CrossVal', 'KFold', 'Holdout', 'Leaveout' and 'CVPartition' is given. A cross-validated model describes no single fit, so FitInfo is not available beside it.

See also: ClassificationKernel, ClassificationLinear, fitclinear

Source Code: fitckernel

Fit a Gaussian kernel classifier to the two overlapping iris species and read what the optimization did.

 load fisheriris
 X = meas(51:end,:);
 Y = species(51:end);
 [Mdl, FitInfo] = fitckernel (X, Y)
Mdl =

  ClassificationKernel

              ResponseName: 'Y'
                ClassNames: {'versicolor'  'virginica'}
                   Learner: 'svm'
    NumExpansionDimensions: 128
               KernelScale: 1
                    Lambda: 0.01
             BoxConstraint: 1

FitInfo =

  scalar structure containing the fields:

    Solver = LBFGS-fast
    LossFunction = hinge
    Lambda = 0.010000
    BetaTolerance = 1.0000e-04
    GradientTolerance = 1.0000e-06
    ObjectiveValue = 0.1805
    GradientMagnitude = 0.020000
    RelativeChangeInBeta = 1.3661e-05
    FitTime = 0
    History = [](0x0)

Fit from a table, and predict on one

 load fisheriris
 inds = ! strcmp (species, 'setosa');
 X = meas(inds,:);
 T = table (X(:,1), X(:,2), X(:,3), X(:,4), ...
            'VariableNames', {'SL', 'SW', 'PL', 'PW'});
 T.Species = categorical (species(inds));

A column holding levels is a categorical predictor without being named one

 T.Wide = categorical (X(:,2) > 2.9, [false true], {'narrow', 'wide'});

The response is named by its column, and everything else is a predictor

 Mdl = fitckernel (T, 'Species');
 Mdl.PredictorNames
ans =
  1x5 cell array

    {'SL'}    {'SW'}    {'PL'}    {'PW'}    {'Wide'}
 Mdl.CategoricalPredictors
ans = 5

A model formula names them instead, holding main effects only

 Mdl2 = fitckernel (T, 'Species ~ PL + Wide');
 Mdl2.PredictorNames
ans =
  1x2 cell array

    {'PL'}    {'Wide'}

predict reads a table by the names the model was fitted on, so the columns may come in any order and may carry more than the model needs

 label = predict (Mdl, T(1:5, [6, 5, 4, 3, 2, 1]));
 label'
ans =
  1x5 categorical array

    versicolor    versicolor    versicolor    versicolor    versicolor