fitclinear — Fit a binary linear classification model.

fitclinear fits a binary linear classifier from a predictor matrix or table and returns a ClassificationLinear object usable with predict.

Syntax

Mdl = fitclinear(X, Y)
Mdl = fitclinear(Tbl, ResponseVarName)
Mdl = fitclinear(Tbl, formula)
Mdl = fitclinear(Tbl, Y)
Mdl = fitclinear(___, Name, Value)
[Mdl, FitInfo] = fitclinear(___)

Inputs

NameTypeRequiredDefaultDescription
tblOrXAnyYesInput table or predictor matrix.
yOrResponseAnyNoResponse vector, response variable name, or table formula.
optionsAnyVariadicName-value options such as Learner, Lambda, Regularization, Solver, ObservationsIn, FitBias, ClassNames, PredictorNames, ResponseName, Weights, Prior, and ScoreTransform.

Returns

NameTypeDescription
MdlAnyClassificationLinear object containing coefficients, class names, and fit metadata.
MdlAnyClassificationLinear object.
FitInfoAnyTraining diagnostics for each Lambda value.

Returned values from fitclinear depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:fitclinear:InvalidArgumentInputs, binary response labels, dimensions, or name-value options are malformed or unsupported.fitclinear: invalid argument
RunMat:fitclinear:InternalRunMat cannot construct the ClassificationLinear result.fitclinear: internal error

How fitclinear works

  • fitclinear(X,Y) fits a binary SVM-style linear classifier with observations in rows of X and labels in Y.
  • Table input supports fitclinear(Tbl,ResponseVarName), fitclinear(Tbl,formula), and fitclinear(Tbl,Y). Formula support covers additive predictor lists such as Y ~ A + B.
  • Learner supports svm and logistic. Logistic models use logistic-loss training and return posterior-style scores by default; SVM models return raw signed-margin scores.
  • Regularization supports ridge and lasso. Lambda accepts a nonnegative scalar or vector, producing one coefficient column per Lambda value.
  • ObservationsIn supports rows and columns for numeric matrices. Table input requires row observations.
  • Y can contain numeric, logical, string, char, or cell-string labels. The predicted label output preserves the label family.
  • Name-value options ClassNames, PredictorNames, ResponseName, Weights, Prior, FitBias, Beta, Bias, Solver, IterationLimit, PassLimit, and ScoreTransform are accepted.
  • Rows containing NaN in predictors or response, or zero weights, are omitted before fitting; Inf values and negative weights are rejected.
  • Cross-validation, cost matrices, leaveout/holdout partitions, and hyperparameter optimization are not implemented yet and raise explicit errors instead of silently producing partial models.
  • The returned object exposes Beta, Bias, Lambda, Learner, Regularization, Solver, ScoreTransform, ResponseName, PredictorNames, ClassNames, Prior, NumObservations, NumPredictors, and ModelParameters properties. [Mdl,FitInfo] returns training diagnostics including Lambda, Objective, NumIterations, NumPasses, and GradientNorm.

Examples

Fit and predict a logistic linear classifier

X = [0; 1; 2; 3];
Y = [0; 0; 1; 1];
[Mdl,FitInfo] = fitclinear(X, Y, 'Learner', 'logistic', 'Lambda', 0);
[label,score] = predict(Mdl, [0.2; 2.8])

Expected output:

label is [0; 1], and score contains one probability column per class.

Use column-oriented observations

Mdl = fitclinear([0 1 2 3], Y, 'ObservationsIn', 'columns');
label = predict(Mdl, [0.2 2.8], 'ObservationsIn', 'columns')

Expected output:

The predictor matrix is transposed internally so columns are observations.

Fit a table-input classifier

T = table(A, B, Y, 'VariableNames', {'A','B','Y'});
Mdl = fitclinear(T, 'Y ~ A + B', 'Learner', 'svm')

Expected output:

Mdl uses A and B as numeric predictors and Y as the binary response.

Using fitclinear with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how fitclinear changes the result.

Run a small fitclinear example, explain the result, then change one input and compare the output.

FAQ

Does fitclinear support multiclass classification?

No. MATLAB's fitclinear is binary; RunMat requires exactly two observed classes and raises an explicit error otherwise.

What does predict return for ClassificationLinear?

predict(Mdl,Xnew) returns predicted class labels. [label,score] = predict(Mdl,Xnew) also returns raw class scores for SVM models or posterior-style probabilities for logistic models.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fitclinear is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.