Skip to content

ml_SVM

mlclassOpenCV 5.0.0
import { ml_SVM } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

Native object: release it with using or delete(). Factories can return null; check before calling methods. Inherits ml_StatModel.


Support Vector Machines * *************************************************************************************** Support Vector Machines.

See: ml_intro_svm

Constructors and members

static getDefaultGridPtr

Generates a grid for %SVM parameters.

getDefaultGridPtr(param_id: number): ml_ParamGrid | null;
param_id

%SVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID.

The function generates a grid pointer for the specified parameter of the %SVM algorithm. The grid may be passed to the function SVM::trainAuto.

Returns

The ml_ParamGrid | null result.

static create

Creates empty model. Use StatModel::train to train the model. Since %SVM has several parameters, you may want to find the best parameters for your problem, it can be done with SVM::trainAuto.

create(): ml_SVM | null;
Returns

The ml_SVM | null result.

static load

Loads and creates a serialized svm from a file

Use SVM::save to serialize and store an SVM to disk. Load the SVM from this file again, by calling this function with the path to the file.

load(filepath: EmbindString): ml_SVM | null;
filepath

path to serialized svm

Returns

The ml_SVM | null result.

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

getType

Type of a %SVM formulation. See SVM::Types. Default value is SVM::C_SVC. See: setType

getType(): number;
Returns

The number result.

setType

Type of a %SVM formulation. See SVM::Types. Default value is SVM::C_SVC. See: setType See: getType

setType(val: number): void;
val

val argument (number).

getGamma

Parameter \gamma of a kernel function. For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1. See: setGamma

getGamma(): number;
Returns

The number result.

setGamma

Parameter \gamma of a kernel function. For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1. See: setGamma See: getGamma

setGamma(val: number): void;
val

val argument (number).

getCoef0

Parameter coef0 of a kernel function. For SVM::POLY or SVM::SIGMOID. Default value is 0. See: setCoef0

getCoef0(): number;
Returns

The number result.

setCoef0

Parameter coef0 of a kernel function. For SVM::POLY or SVM::SIGMOID. Default value is 0. See: setCoef0 See: getCoef0

setCoef0(val: number): void;
val

val argument (number).

getDegree

Parameter degree of a kernel function. For SVM::POLY. Default value is 0. See: setDegree

getDegree(): number;
Returns

The number result.

setDegree

Parameter degree of a kernel function. For SVM::POLY. Default value is 0. See: setDegree See: getDegree

setDegree(val: number): void;
val

val argument (number).

getC

Parameter C of a %SVM optimization problem. For SVM::C_SVC, SVM::EPS_SVR or SVM::NU_SVR. Default value is 0. See: setC

getC(): number;
Returns

The number result.

setC

Parameter C of a %SVM optimization problem. For SVM::C_SVC, SVM::EPS_SVR or SVM::NU_SVR. Default value is 0. See: setC See: getC

setC(val: number): void;
val

val argument (number).

getNu

Parameter \nu of a %SVM optimization problem. For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0. See: setNu

getNu(): number;
Returns

The number result.

setNu

Parameter \nu of a %SVM optimization problem. For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0. See: setNu See: getNu

setNu(val: number): void;
val

val argument (number).

getP

Parameter \epsilon of a %SVM optimization problem. For SVM::EPS_SVR. Default value is 0. See: setP

getP(): number;
Returns

The number result.

setP

Parameter \epsilon of a %SVM optimization problem. For SVM::EPS_SVR. Default value is 0. See: setP See: getP

setP(val: number): void;
val

val argument (number).

getClassWeights

Optional weights in the SVM::C_SVC problem, assigned to particular classes. They are multiplied by C so the parameter C of class i becomes classWeights(i) * C. Thus these weights affect the misclassification penalty for different classes. The larger weight, the larger penalty on misclassification of data from the corresponding class. Default value is empty Mat. See: setClassWeights

getClassWeights(): Mat;
Returns

The Mat result. Release returned native handles with using or delete(), including handles nested in results.

setClassWeights

Optional weights in the SVM::C_SVC problem, assigned to particular classes. They are multiplied by C so the parameter C of class i becomes classWeights(i) * C. Thus these weights affect the misclassification penalty for different classes. The larger weight, the larger penalty on misclassification of data from the corresponding class. Default value is empty Mat. See: setClassWeights See: getClassWeights

setClassWeights(val: Mat): void;
val

val argument (Mat).

getTermCriteria

Termination criteria of the iterative %SVM training procedure which solves a partial case of constrained quadratic optimization problem. You can specify tolerance and/or the maximum number of iterations. Default value is TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, FLT_EPSILON ); See: setTermCriteria

getTermCriteria(): TermCriteria;
Returns

The TermCriteria result.

setTermCriteria

Termination criteria of the iterative %SVM training procedure which solves a partial case of constrained quadratic optimization problem. You can specify tolerance and/or the maximum number of iterations. Default value is TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, FLT_EPSILON ); See: setTermCriteria See: getTermCriteria

setTermCriteria(val: TermCriteria): void;
val

val argument (TermCriteria).

getKernelType

Type of a %SVM kernel. See SVM::KernelTypes. Default value is SVM::RBF.

getKernelType(): number;
Returns

The number result.

setKernel

Initialize with one of predefined kernels. See SVM::KernelTypes.

setKernel(kernelType: number): void;
kernelType

kernel type argument (number).

trainAuto

Trains an %SVM with optimal parameters

trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null, gammaGrid: ml_ParamGrid | null, pGrid: ml_ParamGrid | null, nuGrid: ml_ParamGrid | null, coeffGrid: ml_ParamGrid | null, degreeGrid: ml_ParamGrid | null, balanced: boolean): boolean;
9 available overloads
trainAuto(samples: Mat, layout: number, responses: Mat): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null, gammaGrid: ml_ParamGrid | null): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null, gammaGrid: ml_ParamGrid | null, pGrid: ml_ParamGrid | null): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null, gammaGrid: ml_ParamGrid | null, pGrid: ml_ParamGrid | null, nuGrid: ml_ParamGrid | null): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null, gammaGrid: ml_ParamGrid | null, pGrid: ml_ParamGrid | null, nuGrid: ml_ParamGrid | null, coeffGrid: ml_ParamGrid | null): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null, gammaGrid: ml_ParamGrid | null, pGrid: ml_ParamGrid | null, nuGrid: ml_ParamGrid | null, coeffGrid: ml_ParamGrid | null, degreeGrid: ml_ParamGrid | null): boolean;
trainAuto(samples: Mat, layout: number, responses: Mat, kFold: number, Cgrid: ml_ParamGrid | null, gammaGrid: ml_ParamGrid | null, pGrid: ml_ParamGrid | null, nuGrid: ml_ParamGrid | null, coeffGrid: ml_ParamGrid | null, degreeGrid: ml_ParamGrid | null, balanced: boolean): boolean;
samples

training samples

layout

See ml::SampleTypes.

responses

vector of responses associated with the training samples.

kFold

Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm is

Cgrid

grid for C

gammaGrid

grid for gamma

pGrid

grid for p

nuGrid

grid for nu

coeffGrid

grid for coeff

degreeGrid

grid for degree

balanced

If true and the problem is 2-class classification then the method creates more balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset.

The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal.

This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options.

This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.

Returns

The boolean result.

getSupportVectors

Retrieves all the support vectors

The method returns all the support vectors as a floating-point matrix, where support vectors are
stored as matrix rows.
getSupportVectors(): Mat;
Returns

The Mat result. Release returned native handles with using or delete(), including handles nested in results.

getUncompressedSupportVectors

Retrieves all the uncompressed support vectors of a linear %SVM

The method returns all the uncompressed support vectors of a linear %SVM that the compressed
support vector, used for prediction, was derived from. They are returned in a floating-point
matrix, where the support vectors are stored as matrix rows.
getUncompressedSupportVectors(): Mat;
Returns

The Mat result. Release returned native handles with using or delete(), including handles nested in results.

getDecisionFunction

Retrieves the decision function

getDecisionFunction(i: number, alpha: Mat, svidx: Mat): number;
i

the index of the decision function. If the problem solved is regression, 1-class or 2-class classification, then there will be just one decision function and the index should always be 0. Otherwise, in the case of N-class classification, there will be N(N-1)/2 decision functions.

alpha

Output destination, filled by the native operation. the optional output vector for weights, corresponding to different support vectors. In the case of linear %SVM all the alpha's will be 1's.

svidx

Output destination, filled by the native operation. the optional output vector of indices of support vectors within the matrix of support vectors (which can be retrieved by SVM::getSupportVectors). In the case of linear %SVM each decision function consists of a single "compressed" support vector.

The method returns rho parameter of the decision function, a scalar subtracted from the weighted sum of kernel responses.

Returns

The number result.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.