Table of Contents

Class TrainData

Namespace
OpenCvSharp.ML
Assembly
OpenCvSharp.dll

Training data used by the ml algorithms

public class TrainData : CvPtrObject, IDisposable
Inheritance
TrainData
Implements
Inherited Members

Methods

Create(InputArray, SampleTypes, InputArray, InputArray, InputArray, InputArray, InputArray)

Creates training data from in-memory arrays.

public static TrainData Create(InputArray samples, SampleTypes layout, InputArray responses, InputArray varIdx = default, InputArray sampleIdx = default, InputArray sampleWeights = default, InputArray varType = default)

Parameters

samples InputArray

matrix of samples. It should have CV_32F type.

layout SampleTypes

see ml::SampleTypes.

responses InputArray

matrix of responses. If the responses are scalar, they should be stored as a single row or as a single column. The matrix should have type CV_32F or CV_32S (in the former case the responses are considered as ordered by default; in the latter case - as categorical)

varIdx InputArray

vector specifying which variables to use for training. It can be an integer vector (CV_32S) containing 0-based variable indices or byte vector (CV_8U) containing a mask of active variables.

sampleIdx InputArray

vector specifying which samples to use for training. It can be an integer vector (CV_32S) containing 0-based sample indices or byte vector (CV_8U) containing a mask of training samples.

sampleWeights InputArray

optional vector with weights for each sample. It should have CV_32F type.

varType InputArray

optional vector of type CV_8U and size <number_of_variables_in_samples> + <number_of_variables_in_responses>, containing types of each input and output variable. See ml::VariableTypes.

Returns

TrainData

GetClassLabels()

Returns the vector of class labels

public Mat GetClassLabels()

Returns

Mat

GetLayout()

Returns the layout (ml::SampleTypes) used by this training data.

public SampleTypes GetLayout()

Returns

SampleTypes

GetMissing()

Returns the mask of missing values in the samples

public Mat GetMissing()

Returns

Mat

GetNAllVars()

Returns the number of variables including the responses

public int GetNAllVars()

Returns

int

GetNSamples()

Returns the total number of samples

public int GetNSamples()

Returns

int

GetNTestSamples()

Returns the number of test samples

public int GetNTestSamples()

Returns

int

GetNTrainSamples()

Returns the number of training samples

public int GetNTrainSamples()

Returns

int

GetNVars()

Returns the number of variables

public int GetNVars()

Returns

int

GetResponses()

Returns the vector of responses

public Mat GetResponses()

Returns

Mat

GetSamples()

Returns the matrix of all the samples

public Mat GetSamples()

Returns

Mat

GetTestResponses()

Returns the vector of responses for the test samples

public Mat GetTestResponses()

Returns

Mat

GetTestSampleIdx()

Returns the indices of the test samples

public Mat GetTestSampleIdx()

Returns

Mat

GetTestSamples()

Returns matrix of test samples

public Mat GetTestSamples()

Returns

Mat

GetTrainResponses()

Returns the vector of responses for the training samples

public Mat GetTrainResponses()

Returns

Mat

GetTrainSampleIdx()

Returns the indices of the training samples

public Mat GetTrainSampleIdx()

Returns

Mat

GetTrainSamples(SampleTypes, bool, bool)

Returns matrix of train samples

public Mat GetTrainSamples(SampleTypes layout = SampleTypes.RowSample, bool compressSamples = true, bool compressVars = true)

Parameters

layout SampleTypes

The requested layout. If it's different from the initial one, the matrix is transposed. See ml::SampleTypes.

compressSamples bool

if true, the function returns only the training samples (specified by sampleIdx)

compressVars bool

if true, the function returns the shorter training samples, containing only the active variables.

Returns

Mat

GetVarIdx()

Returns the vector of variable indices used for training

public Mat GetVarIdx()

Returns

Mat

GetVarType()

Returns the type of each input and output variable

public Mat GetVarType()

Returns

Mat

LoadFromCSV(string, int, int, int, string, char, char)

Reads the dataset from a .csv file and returns the ready-to-use training data.

public static TrainData LoadFromCSV(string filename, int headerLineCount, int responseStartIdx = -1, int responseEndIdx = -1, string varTypeSpec = "", char delimiter = ',', char missch = '?')

Parameters

filename string

The input file name

headerLineCount int

The number of lines in the beginning to skip; besides the header, the function also skips empty lines and lines starting with '#'

responseStartIdx int

Index of the first output variable. If -1, the function considers the last variable as the response

responseEndIdx int

Index of the last output variable + 1. If -1, then there is single response variable at responseStartIdx.

varTypeSpec string

The optional text string that specifies the variables' types. It has the format ord[n1-n2,n3,n4-n5,...]cat[n6,n7-n8,...].

delimiter char

The character used to separate values in each line.

missch char

The character used to specify missing measurements. It should not be a digit.

Returns

TrainData

SetTrainTestSplit(int, bool)

Splits the training data into the training and test parts

public void SetTrainTestSplit(int count, bool shuffle = true)

Parameters

count int
shuffle bool

SetTrainTestSplitRatio(double, bool)

Splits the training data into the training and test parts

public void SetTrainTestSplitRatio(double ratio, bool shuffle = true)

Parameters

ratio double
shuffle bool

ShuffleTrainTest()

Shuffles the training and test sample indices

public void ShuffleTrainTest()