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
pnint
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
ClassWeights
Optional weights in the SVM::C_SVC problem, assigned to particular classes.
public Mat ClassWeights { get; set; }
Property Value
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
Degree
Parameter degree of a kernel function. For SVM::POLY. Default value is 0.
public double Degree { get; set; }
Property Value
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
KernelType
Type of a %SVM kernel. See SVM::KernelTypes. Default value is SVM::RBF.
public SVM.KernelTypes KernelType { get; set; }
Property Value
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
P
Parameter epsilon of a %SVM optimization problem. For SVM::EPS_SVR. Default value is 0.
public double P { get; set; }
Property Value
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
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
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
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
iinti 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.
alphaOutputArrayalpha 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.
svidxOutputArraythe 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
GetDefaultGrid(ParamTypes)
Generates a grid for SVM parameters.
public static ParamGrid GetDefaultGrid(SVM.ParamTypes paramId)
Parameters
paramIdSVM.ParamTypesSVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID.
Returns
GetSupportVectors()
Retrieves all the support vectors
public Mat GetSupportVectors()
Returns
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
filePathstring
Returns
LoadFromString(string)
Loads algorithm from a String.
public static SVM LoadFromString(string strModel)
Parameters
strModelstringThe string variable containing the model you want to load.
Returns
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
dataTrainDatathe training data that can be constructed using TrainData::create or TrainData::loadFromCSV.
kFoldintCross-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.
cGridParamGrid?grid for C
gammaGridParamGrid?grid for gamma
pGridParamGrid?grid for p
nuGridParamGrid?grid for nu
coeffGridParamGrid?grid for coeff
degreeGridParamGrid?grid for degree
balancedboolIf 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.