classify — Classify observations using discriminant analysis.
classify assigns each row of sample to a class learned from rows of training and labels in group using discriminant analysis.
Syntax
class = classify(sample, training, group)
class = classify(sample, training, group, type, prior)
[class,err,posterior,logp,coeff] = classify(___)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
sample | NumericArray | Yes | — | Sample observations in rows. |
training | NumericArray | Yes | — | Training observations in rows. |
group | Any | Yes | — | Training group labels. |
type | StringScalar | No | linear | Discriminant type: linear, quadratic, diagLinear, diagQuadratic, or mahalanobis. |
prior | Any | No | — | Prior probabilities as a numeric vector, 'empirical', or a struct with group and prob fields. |
Returns
| Name | Type | Description |
|---|---|---|
class | Any | Predicted class labels for sample observations. |
err | NumericScalar | Apparent training-set error rate. |
posterior | NumericArray | Posterior class probabilities for sample observations. |
logp | NumericArray | Log unconditional density for sample observations. |
coeff | Any | Pairwise discriminant boundary coefficients. |
Returned values from classify depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:classify:InvalidArgument | Inputs, labels, dimensions, discriminant type, or priors are malformed. | classify: invalid argument |
RunMat:classify:Numerical | Covariance matrices cannot be solved numerically. | classify: numerical failure |
RunMat:classify:Internal | RunMat cannot construct classification outputs. | classify: internal error |
How classify works
classify(sample,training,group)uses linear discriminant analysis with equal class priors.classify(sample,training,group,type)supportslinear,quadratic,diagLinear,diagQuadratic, andmahalanobisdiscriminants.classify(sample,training,group,type,prior)accepts priors as a numeric vector, the stringempirical, or a struct withgroupandprobfields.sampleandtrainingmust be finite numeric 2-D matrices with the same number of columns; observations are stored in rows.groupcan be numeric, logical, string, character-row, or cell-string labels. Missing numeric labels (NaN) and empty or<missing>text labels are ignored during fitting.- Predicted labels preserve the input label family: numeric vectors return numeric labels, logical labels return logical arrays, string labels return string arrays, character-row labels return character matrices, and cell-string labels return cell arrays of character vectors.
- With multiple outputs,
[class,err,posterior,logp,coeff]returns prior-weighted apparent training error, posterior probabilities, unconditional log densities, and a pairwise discriminant coefficient struct array. mahalanobisclassification returnsNaNposterior and log-density values because it is distance-based rather than prior-probability-based.
Examples
Classify two samples with linear discriminants
training = [0; 0.5; 2; 2.5];
group = [1; 1; 2; 2];
[class,err,posterior] = classify([0.2; 2.2], training, group)Expected output:
class is [1; 2], err is 0 for the separable training data, and posterior is a 2-by-2 matrix.Use text labels and an empirical prior
group = ["low"; "low"; "high"; "high"];
class = classify([0.1; 3.2], [0; 0.4; 3; 3.4], group, 'quadratic', 'empirical')Expected output:
class is a string array containing "low" and "high".Using classify with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how classify changes the result.
Run a small classify example, explain the result, then change one input and compare the output.
FAQ
Does classify train a reusable model object?⌄
No. It performs one-shot discriminant classification from the supplied training data. Use model-fitting helpers such as fitctree when a reusable object is needed.
Are priors used with the mahalanobis type?⌄
No. Mahalanobis classification is based on class distance; prior values are parsed for compatibility but do not affect class assignment.
Related Stats functions
Ml
bayesopt · confusionmat · crossvalind · cvpartition · fitclinear · 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 classify is executed, line by line, in Rust.
- View the source for classify 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.