Categories &

Functions List

Function Reference: fitclinear

statistics: Mdl = fitclinear (X, Y)

statistics: Mdl = fitclinear (Tbl, ResponseVarName)

statistics: Mdl = fitclinear (Tbl, formula)

statistics: Mdl = fitclinear (Tbl, Y)

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

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

Fit a linear binary classifier.

Mdl = fitclinear (X, Y) returns a ClassificationLinear 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 = fitclinear (…, name, value) passes the given Name-Value pairs to the model. They are documented under ClassificationLinear, and the ones most often wanted are 'Learner', 'Regularization', 'Lambda', 'Solver' and 'ObservationsIn'.

[Mdl, FitInfo] = fitclinear (…) also returns a structure describing the optimization: what it converged to, how far it got, and which tolerance stopped it. Its fields follow the solver, so a dual fit reports the dual variables and a mini-batch fit the batch it stopped on.

Mdl = fitclinear (…, cvopt, value) returns a ClassificationPartitionedLinear 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: ClassificationLinear, ClassificationKernel, fitckernel

Source Code: fitclinear

Fit a linear 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] = fitclinear (X, Y, 'Learner', 'logistic')
Mdl =

  ClassificationLinear

             ResponseName: 'Y'
               ClassNames: {'versicolor'  'virginica'}
           ScoreTransform: 'logit'
                     Beta: [4x1 double]
                     Bias: -14.4308
                   Lambda: 0.01
                  Learner: 'logistic'

FitInfo =

  scalar structure containing the fields:

    Lambda = 0.010000
    Objective = 0.2405
    IterationLimit = 1000
    NumIterations = 51
    GradientNorm = 7.1054e-07
    GradientTolerance = 1.0000e-06
    RelativeChangeInBeta = 9.1709e-06
    BetaTolerance = 1.0000e-04
    DeltaGradient = [](0x0)
    DeltaGradientTolerance = [](0x0)
    TerminationCode = 1
    TerminationStatus =
    {
      [1,1] = Tolerance on coefficients satisfied.
    }

    History = [](0x0)
    FitTime = 0
    Solver =
    {
      [1,1] = bfgs
    }

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 = species(inds);

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

 Mdl = fitclinear (T, 'Species');
 Mdl.PredictorNames
ans =
  1x4 cell array

    {'SL'}    {'SW'}    {'PL'}    {'PW'}
 Mdl.ResponseName
ans = Species

predict reads a table by the names the model was fitted on, so the columns may come in any order

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

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