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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tblOrX | Any | Yes | — | Input table or predictor matrix. |
yOrResponse | Any | No | — | Response vector, response variable name, or table formula. |
options | Any | Variadic | — | Name-value options such as Learner, Lambda, Regularization, Solver, ObservationsIn, FitBias, ClassNames, PredictorNames, ResponseName, Weights, Prior, and ScoreTransform. |
Returns
| Name | Type | Description |
|---|---|---|
Mdl | Any | ClassificationLinear object containing coefficients, class names, and fit metadata. |
Mdl | Any | ClassificationLinear object. |
FitInfo | Any | Training diagnostics for each Lambda value. |
Returned values from fitclinear depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:fitclinear:InvalidArgument | Inputs, binary response labels, dimensions, or name-value options are malformed or unsupported. | fitclinear: invalid argument |
RunMat:fitclinear:Internal | RunMat 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 ofXand labels inY.- Table input supports
fitclinear(Tbl,ResponseVarName),fitclinear(Tbl,formula), andfitclinear(Tbl,Y). Formula support covers additive predictor lists such asY ~ A + B. Learnersupportssvmandlogistic. Logistic models use logistic-loss training and return posterior-style scores by default; SVM models return raw signed-margin scores.Regularizationsupportsridgeandlasso.Lambdaaccepts a nonnegative scalar or vector, producing one coefficient column per Lambda value.ObservationsInsupportsrowsandcolumnsfor numeric matrices. Table input requires row observations.Ycan 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, andScoreTransformare accepted. - Rows containing
NaNin predictors or response, or zero weights, are omitted before fitting;Infvalues 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, andModelParametersproperties.[Mdl,FitInfo]returns training diagnostics includingLambda,Objective,NumIterations,NumPasses, andGradientNorm.
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.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fitclinear is executed, line by line, in Rust.
- View the source for fitclinear in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.