Table of Contents

Class SVM

Namespace
OpenCvSharp.ML
Assembly
OpenCvSharp.dll

Support Vector Machines

public class SVM : StatModel, IDisposable, ICvPtrHolder
Inheritance
SVM
Implements
Inherited Members

Constructors

SVM(nint)

Creates instance by raw pointer cv::ml::SVM*

protected SVM(nint p)

Parameters

p nint

Properties

C

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

public double C { get; set; }

Property Value

double

ClassWeights

Optional weights in the SVM::C_SVC problem, assigned to particular classes.

public Mat ClassWeights { get; set; }

Property Value

Mat

Remarks

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.

Coef0

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

public double Coef0 { get; set; }

Property Value

double

Degree

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

public double Degree { get; set; }

Property Value

double

Gamma

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

public double Gamma { get; set; }

Property Value

double

KernelType

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

public SVM.KernelTypes KernelType { get; set; }

Property Value

SVM.KernelTypes

Nu

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

public double Nu { get; set; }

Property Value

double

P

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

public double P { get; set; }

Property Value

double

TermCriteria

Termination criteria of the iterative SVM training procedure which solves a partial case of constrained quadratic optimization problem.

public TermCriteria TermCriteria { get; set; }

Property Value

TermCriteria

Remarks

You can specify tolerance and/or the maximum number of iterations. Default value is TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, FLT_EPSILON );

Type

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

public SVM.Types Type { get; set; }

Property Value

SVM.Types

Methods

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.

public static SVM Create()

Returns

SVM

DisposeManaged()

Releases managed resources

protected override void DisposeManaged()

GetDecisionFunction(int, OutputArray, OutputArray)

Retrieves the decision function

public double GetDecisionFunction(int i, OutputArray alpha, OutputArray svidx)

Parameters

i int

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 OutputArray

alpha 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 OutputArray

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.

Returns

double

GetDefaultGrid(ParamTypes)

Generates a grid for SVM parameters.

public static ParamGrid GetDefaultGrid(SVM.ParamTypes paramId)

Parameters

paramId SVM.ParamTypes

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

Returns

ParamGrid

GetSupportVectors()

Retrieves all the support vectors

public Mat GetSupportVectors()

Returns

Mat

Load(string)

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.

public static SVM Load(string filePath)

Parameters

filePath string

Returns

SVM

LoadFromString(string)

Loads algorithm from a String.

public static SVM LoadFromString(string strModel)

Parameters

strModel string

The string variable containing the model you want to load.

Returns

SVM

TrainAuto(TrainData, int, ParamGrid?, ParamGrid?, ParamGrid?, ParamGrid?, ParamGrid?, ParamGrid?, bool)

Trains an %SVM with optimal parameters.

public bool TrainAuto(TrainData data, int kFold = 10, ParamGrid? cGrid = null, ParamGrid? gammaGrid = null, ParamGrid? pGrid = null, ParamGrid? nuGrid = null, ParamGrid? coeffGrid = null, ParamGrid? degreeGrid = null, bool balanced = false)

Parameters

data TrainData

the training data that can be constructed using TrainData::create or TrainData::loadFromCSV.

kFold int

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 executed kFold times.

cGrid ParamGrid?

grid for C

gammaGrid ParamGrid?

grid for gamma

pGrid ParamGrid?

grid for p

nuGrid ParamGrid?

grid for nu

coeffGrid ParamGrid?

grid for coeff

degreeGrid ParamGrid?

grid for degree

balanced bool

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.

Returns

bool