Skip to content

ml_TrainData

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

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

ARGUMENTSConstructor or factory
CLASSml_TrainData
RETURN TYPEOwned native handle
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

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

Class encapsulating training data.

Please note that the class only specifies the interface of training data, but not implementation. All the statistical model classes in ml module accepts Ptr<TrainData> as parameter. In other words, you can create your own class derived from TrainData and pass smart pointer to the instance of this class into StatModel::train.

See: ml_intro_data

Constructors and members

static getSubVector

Extract from 1D vector elements specified by passed indexes.

getSubVector(vec: Mat, idx: Mat): Mat;
vec

input vector (supported types: CV_32S, CV_32F, CV_64F)

idx

1D index vector

Returns

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

static getSubMatrix

Extract from matrix rows/cols specified by passed indexes.

getSubMatrix(matrix: Mat, idx: Mat, layout: number): Mat;
matrix

input matrix (supported types: CV_32S, CV_32F, CV_64F)

idx

1D index vector

layout

specifies to extract rows (cv::ml::ROW_SAMPLES) or to extract columns (cv::ml::COL_SAMPLES)

Returns

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

static create

Creates training data from in-memory arrays.

create(samples: Mat, layout: number, responses: Mat, varIdx: Mat, sampleIdx: Mat, sampleWeights: Mat, varType: Mat): ml_TrainData | null;
5 available overloads
create(samples: Mat, layout: number, responses: Mat): ml_TrainData | null;
create(samples: Mat, layout: number, responses: Mat, varIdx: Mat): ml_TrainData | null;
create(samples: Mat, layout: number, responses: Mat, varIdx: Mat, sampleIdx: Mat): ml_TrainData | null;
create(samples: Mat, layout: number, responses: Mat, varIdx: Mat, sampleIdx: Mat, sampleWeights: Mat): ml_TrainData | null;
create(samples: Mat, layout: number, responses: Mat, varIdx: Mat, sampleIdx: Mat, sampleWeights: Mat, varType: Mat): ml_TrainData | null;
samples

matrix of samples. It should have CV_32F type.

layout

see ml::SampleTypes.

responses

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

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

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

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

varType

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

The ml_TrainData | 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.

getLayout

Return ROW_SAMPLE or COL_SAMPLE to indicate how samples are arranged in the data matrix.

getLayout(): number;
Returns

The number result.

getNTrainSamples

Return the number of samples in the training partition.

getNTrainSamples(): number;
Returns

The number result.

getNTestSamples

Return the number of samples in the test partition.

getNTestSamples(): number;
Returns

The number result.

getNSamples

Return the total number of selected samples.

getNSamples(): number;
Returns

The number result.

getNVars

Return the number of selected input variables per sample.

getNVars(): number;
Returns

The number result.

getNAllVars

Return the number of input variables before variable selection.

getNAllVars(): number;
Returns

The number result.

getSample

Return the sample configured on this ml_TrainData object.

getSample(indices: Mat, index: number): FloatVector;
indices

indices argument (Mat).

index

index argument (number).

Returns

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

getSamples

Return an owned matrix header for the input sample data in its original layout.

getSamples(): Mat;
Returns

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

getMissing

Return the matrix marking missing values in the input samples.

getMissing(): Mat;
Returns

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

getTrainSamples

Returns matrix of train samples

getTrainSamples(layout: number, compressSamples: boolean, compressVars: boolean): Mat;
4 available overloads
getTrainSamples(): Mat;
getTrainSamples(layout: number): Mat;
getTrainSamples(layout: number, compressSamples: boolean): Mat;
getTrainSamples(layout: number, compressSamples: boolean, compressVars: boolean): Mat;
layout

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

compressSamples

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

compressVars

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

In current implementation the function tries to avoid physical data copying and returns the matrix stored inside TrainData (unless the transposition or compression is needed).

Returns

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

getTrainResponses

Returns the vector of responses

The function returns ordered or the original categorical responses. Usually it's used in
regression algorithms.
getTrainResponses(): Mat;
Returns

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

getTrainNormCatResponses

Returns the vector of normalized categorical responses

The function returns vector of responses. Each response is integer from `0` to `<number of
classes>-1`. The actual label value can be retrieved then from the class label vector, see
TrainData::getClassLabels.
getTrainNormCatResponses(): Mat;
Returns

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

getTestResponses

Return the test responses configured on this ml_TrainData object.

getTestResponses(): Mat;
Returns

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

getTestNormCatResponses

Return the test norm cat responses configured on this ml_TrainData object.

getTestNormCatResponses(): Mat;
Returns

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

getResponses

Return the responses configured on this ml_TrainData object.

getResponses(): Mat;
Returns

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

getNormCatResponses

Return the norm cat responses configured on this ml_TrainData object.

getNormCatResponses(): Mat;
Returns

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

getSampleWeights

Return the sample weights configured on this ml_TrainData object.

getSampleWeights(): Mat;
Returns

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

getTrainSampleWeights

Return the train sample weights configured on this ml_TrainData object.

getTrainSampleWeights(): Mat;
Returns

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

getTestSampleWeights

Return the test sample weights configured on this ml_TrainData object.

getTestSampleWeights(): Mat;
Returns

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

getVarIdx

Return the var idx configured on this ml_TrainData object.

getVarIdx(): Mat;
Returns

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

getVarType

Return the var type configured on this ml_TrainData object.

getVarType(): Mat;
Returns

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

getVarSymbolFlags

Return the var symbol flags configured on this ml_TrainData object.

getVarSymbolFlags(): Mat;
Returns

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

getResponseType

Return the response type configured on this ml_TrainData object.

getResponseType(): number;
Returns

The number result.

getTrainSampleIdx

Return the train sample idx configured on this ml_TrainData object.

getTrainSampleIdx(): Mat;
Returns

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

getTestSampleIdx

Return the test sample idx configured on this ml_TrainData object.

getTestSampleIdx(): Mat;
Returns

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

getValues

Return the values configured on this ml_TrainData object.

getValues(index: number, indices: Mat): FloatVector;
index

index argument (number).

indices

indices argument (Mat).

Returns

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

getDefaultSubstValues

Return the default subst values configured on this ml_TrainData object.

getDefaultSubstValues(): Mat;
Returns

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

getCatCount

Return the cat count configured on this ml_TrainData object.

getCatCount(vi: number): number;
vi

vi argument (number).

Returns

The number result.

getClassLabels

Returns the vector of class labels

The function returns vector of unique labels occurred in the responses.
getClassLabels(): Mat;
Returns

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

getCatOfs

Return the cat ofs configured on this ml_TrainData object.

getCatOfs(): Mat;
Returns

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

getCatMap

Return the cat map configured on this ml_TrainData object.

getCatMap(): Mat;
Returns

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

setTrainTestSplit

Splits the training data into the training and test parts See: TrainData::setTrainTestSplitRatio

setTrainTestSplit(count: number, shuffle: boolean): void;
2 available overloads
setTrainTestSplit(count: number): void;
setTrainTestSplit(count: number, shuffle: boolean): void;
count

count argument (number).

shuffle

shuffle argument (boolean).

setTrainTestSplitRatio

Splits the training data into the training and test parts

The function selects a subset of specified relative size and then returns it as the training
set. If the function is not called, all the data is used for training. Please, note that for
each of TrainData::getTrain\* there is corresponding TrainData::getTest\*, so that the test
subset can be retrieved and processed as well.
See: TrainData::setTrainTestSplit
setTrainTestSplitRatio(ratio: number, shuffle: boolean): void;
2 available overloads
setTrainTestSplitRatio(ratio: number): void;
setTrainTestSplitRatio(ratio: number, shuffle: boolean): void;
ratio

ratio argument (number).

shuffle

shuffle argument (boolean).

shuffleTrainTest

Randomly repartition the current training and test sample indices while preserving the split sizes.

shuffleTrainTest(): void;

getTestSamples

Returns matrix of test samples

getTestSamples(): Mat;
Returns

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

getNames

Returns vector of symbolic names captured in loadFromCSV()

getNames(names: StringVector): void;
names

names argument (StringVector).

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.