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

NameTypeRequiredDefaultDescription
sampleNumericArrayYesSample observations in rows.
trainingNumericArrayYesTraining observations in rows.
groupAnyYesTraining group labels.
typeStringScalarNolinearDiscriminant type: linear, quadratic, diagLinear, diagQuadratic, or mahalanobis.
priorAnyNoPrior probabilities as a numeric vector, 'empirical', or a struct with group and prob fields.

Returns

NameTypeDescription
classAnyPredicted class labels for sample observations.
errNumericScalarApparent training-set error rate.
posteriorNumericArrayPosterior class probabilities for sample observations.
logpNumericArrayLog unconditional density for sample observations.
coeffAnyPairwise discriminant boundary coefficients.

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

Errors

IdentifierWhenMessage
RunMat:classify:InvalidArgumentInputs, labels, dimensions, discriminant type, or priors are malformed.classify: invalid argument
RunMat:classify:NumericalCovariance matrices cannot be solved numerically.classify: numerical failure
RunMat:classify:InternalRunMat 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) supports linear, quadratic, diagLinear, diagQuadratic, and mahalanobis discriminants.
  • classify(sample,training,group,type,prior) accepts priors as a numeric vector, the string empirical, or a struct with group and prob fields.
  • sample and training must be finite numeric 2-D matrices with the same number of columns; observations are stored in rows.
  • group can 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.
  • mahalanobis classification returns NaN posterior 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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how classify 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.