Skip to content

ml_StatModel

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

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

ARGUMENTSConstructor or factory
CLASSml_StatModel
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. Inherits Algorithm.

Base class for statistical models in OpenCV ML.

Constructors and members

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.

getVarCount

Returns the number of variables in training samples

getVarCount(): number;
Returns

The number result.

empty

Return whether the statistical model has no learned parameters.

empty(): boolean;
Returns

The boolean result.

isTrained

Returns true if the model is trained

isTrained(): boolean;
Returns

The boolean result.

isClassifier

Returns true if the model is classifier

isClassifier(): boolean;
Returns

The boolean result.

train

Trains the statistical model

train(trainData: ml_TrainData | null, flags: number): boolean;
2 available overloads
train(trainData: ml_TrainData | null): boolean;
train(trainData: ml_TrainData | null, flags: number): boolean;
trainData

training data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create.

flags

optional flags, depending on the model. Some of the models can be updated with the new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).

Returns

The boolean result.

train1

Trains the statistical model

train1(samples: Mat, layout: number, responses: Mat): boolean;
samples

training samples

layout

See ml::SampleTypes.

responses

vector of responses associated with the training samples.

Returns

The boolean result.

calcError

Computes error on the training or test dataset

calcError(data: ml_TrainData | null, test: boolean, resp: Mat): number;
data

the training data

test

if true, the error is computed over the test subset of the data, otherwise it's computed over the training subset of the data. Please note that if you loaded a completely different dataset to evaluate already trained classifier, you will probably want not to set the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so that the error is computed for the whole new set. Yes, this sounds a bit confusing.

resp

Output destination, filled by the native operation. the optional output responses.

The method uses StatModel::predict to compute the error. For regression models the error is computed as RMS, for classifiers - as a percent of missclassified samples (0%-100%).

Returns

The number result.

predict

Predicts response(s) for the provided sample(s)

predict(samples: Mat, results: Mat, flags: number): number;
3 available overloads
predict(samples: Mat): number;
predict(samples: Mat, results: Mat): number;
predict(samples: Mat, results: Mat, flags: number): number;
samples

The input samples, floating-point matrix

results

Output destination, filled by the native operation. The optional output matrix of results.

flags

The optional flags, model-dependent. See cv::ml::StatModel::Flags.

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.