ml_EM
import { ml_EM } from '@banou/opencv-wasm'Use after await initOpenCV(). See the initialization and named imports guide.
Native object: release it with using or delete(). Factories can return null; check before calling methods. Inherits ml_StatModel.
Expectation - Maximization *
***************************************************************************************
The class implements the Expectation Maximization algorithm.
See: ml_intro_em
Constructors and members
static create
Creates empty %EM model. The model should be trained then using StatModel::train(traindata, flags) method. Alternatively, you can use one of the EM::train* methods or load it from file using Algorithm::load<EM>(filename).
create(): ml_EM | null;The ml_EM | null result.
static load
Loads and creates a serialized EM from a file
Use EM::save to serialize and store an EM to disk. Load the EM from this file again, by calling this function with the path to the file. Optionally specify the node for the file containing the classifier
load(filepath: EmbindString, nodeName: EmbindString): ml_EM | null;2 available overloads
load(filepath: EmbindString): ml_EM | null;load(filepath: EmbindString, nodeName: EmbindString): ml_EM | null;filepathpath to serialized EM
nodeNamename of node containing the classifier
The ml_EM | 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;The this result.
getClustersNumber
The number of mixture components in the Gaussian mixture model. Default value of the parameter is EM::DEFAULT_NCLUSTERS=5. Some of %EM implementation could determine the optimal number of mixtures within a specified value range, but that is not the case in ML yet. See: setClustersNumber
getClustersNumber(): number;The number result.
setClustersNumber
The number of mixture components in the Gaussian mixture model. Default value of the parameter is EM::DEFAULT_NCLUSTERS=5. Some of %EM implementation could determine the optimal number of mixtures within a specified value range, but that is not the case in ML yet. See: setClustersNumber See: getClustersNumber
setClustersNumber(val: number): void;valval argument (number).
getCovarianceMatrixType
Constraint on covariance matrices which defines type of matrices. See EM::Types. See: setCovarianceMatrixType
getCovarianceMatrixType(): number;The number result.
setCovarianceMatrixType
Constraint on covariance matrices which defines type of matrices. See EM::Types. See: setCovarianceMatrixType See: getCovarianceMatrixType
setCovarianceMatrixType(val: number): void;valval argument (number).
getTermCriteria
The termination criteria of the %EM algorithm. The %EM algorithm can be terminated by the number of iterations termCrit.maxCount (number of M-steps) or when relative change of likelihood logarithm is less than termCrit.epsilon. Default maximum number of iterations is EM::DEFAULT_MAX_ITERS=100. See: setTermCriteria
getTermCriteria(): TermCriteria;The TermCriteria result.
setTermCriteria
The termination criteria of the %EM algorithm. The %EM algorithm can be terminated by the number of iterations termCrit.maxCount (number of M-steps) or when relative change of likelihood logarithm is less than termCrit.epsilon. Default maximum number of iterations is EM::DEFAULT_MAX_ITERS=100. See: setTermCriteria See: getTermCriteria
setTermCriteria(val: TermCriteria): void;valval argument (TermCriteria).
getWeights
Returns weights of the mixtures
Returns vector with the number of elements equal to the number of mixtures.
getWeights(): Mat;The Mat result. Release returned native handles with using or delete(), including handles nested in results.
getMeans
Returns the cluster centers (means of the Gaussian mixture)
Returns matrix with the number of rows equal to the number of mixtures and number of columns
equal to the space dimensionality.
getMeans(): Mat;The Mat result. Release returned native handles with using or delete(), including handles nested in results.
getCovs
Returns covariation matrices
Returns vector of covariation matrices. Number of matrices is the number of gaussian mixtures,
each matrix is a square floating-point matrix NxN, where N is the space dimensionality.
getCovs(covs: MatVector): void;covsOutput destination, filled by the native operation. covs argument (MatVector).
predict
Returns posterior probabilities for the provided samples
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;samplesThe input samples, floating-point matrix
resultsOutput destination, filled by the native operation. The optional output
nSamples \times nClustersmatrix of results. It contains posterior probabilities for each sample from the inputflagsThis parameter will be ignored
The number result.
predict2
Returns a likelihood logarithm value and an index of the most probable mixture component for the given sample.
predict2(sample: Mat, probs: Mat): Vec2d;sampleA sample for classification. It should be a one-channel matrix of
1 \times dimsordims \times 1size.probsOutput destination, filled by the native operation. Optional output matrix that contains posterior probabilities of each component given the sample. It has
1 \times nclusterssize and CV_64FC1 type.The method returns a two-element double vector. Zero element is a likelihood logarithm value for the sample. First element is an index of the most probable mixture component for the given sample.
The Vec2d result.
trainEM
Estimate the Gaussian mixture parameters from a samples set.
This variation starts with Expectation step. Initial values of the model parameters will be
estimated by the k-means algorithm.
Unlike many of the ML models, %EM is an unsupervised learning algorithm and it does not take
responses (class labels or function values) as input. Instead, it computes the *Maximum
Likelihood Estimate* of the Gaussian mixture parameters from an input sample set, stores all the
parameters inside the structure: `p_{i,k}` in probs, `a_k` in means , `S_k` in
covs[k], `\pi_k` in weights , and optionally computes the output "class label" for each
sample: `\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N` (indices of the most
probable mixture component for each sample).
The trained model can be used further for prediction, just like any other classifier. The
trained model is similar to the NormalBayesClassifier.
trainEM(samples: Mat, logLikelihoods: Mat, labels: Mat, probs: Mat): boolean;4 available overloads
trainEM(samples: Mat): boolean;trainEM(samples: Mat, logLikelihoods: Mat): boolean;trainEM(samples: Mat, logLikelihoods: Mat, labels: Mat): boolean;trainEM(samples: Mat, logLikelihoods: Mat, labels: Mat, probs: Mat): boolean;samplesSamples from which the Gaussian mixture model will be estimated. It should be a one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.
logLikelihoodsOutput destination, filled by the native operation. The optional output matrix that contains a likelihood logarithm value for each sample. It has
nsamples \times 1size and CV_64FC1 type.labelsOutput destination, filled by the native operation. The optional output "class label" for each sample:
\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N(indices of the most probable mixture component for each sample). It hasnsamples \times 1size and CV_32SC1 type.probsOutput destination, filled by the native operation. The optional output matrix that contains posterior probabilities of each Gaussian mixture component given the each sample. It has
nsamples \times nclusterssize and CV_64FC1 type.
The boolean result.
trainE
Estimate the Gaussian mixture parameters from a samples set.
This variation starts with Expectation step. You need to provide initial means `a_k` of
mixture components. Optionally you can pass initial weights `\pi_k` and covariance matrices
`S_k` of mixture components.
trainE(samples: Mat, means0: Mat, covs0: Mat, weights0: Mat, logLikelihoods: Mat, labels: Mat, probs: Mat): boolean;6 available overloads
trainE(samples: Mat, means0: Mat): boolean;trainE(samples: Mat, means0: Mat, covs0: Mat): boolean;trainE(samples: Mat, means0: Mat, covs0: Mat, weights0: Mat): boolean;trainE(samples: Mat, means0: Mat, covs0: Mat, weights0: Mat, logLikelihoods: Mat): boolean;trainE(samples: Mat, means0: Mat, covs0: Mat, weights0: Mat, logLikelihoods: Mat, labels: Mat): boolean;trainE(samples: Mat, means0: Mat, covs0: Mat, weights0: Mat, logLikelihoods: Mat, labels: Mat, probs: Mat): boolean;samplesSamples from which the Gaussian mixture model will be estimated. It should be a one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.
means0Initial means
a_kof mixture components. It is a one-channel matrix ofnclusters \times dimssize. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.covs0The vector of initial covariance matrices
S_kof mixture components. Each of covariance matrices is a one-channel matrix ofdims \times dimssize. If the matrices do not have CV_64F type they will be converted to the inner matrices of such type for the further computing.weights0Initial weights
\pi_kof mixture components. It should be a one-channel floating-point matrix with1 \times nclustersornclusters \times 1size.logLikelihoodsOutput destination, filled by the native operation. The optional output matrix that contains a likelihood logarithm value for each sample. It has
nsamples \times 1size and CV_64FC1 type.labelsOutput destination, filled by the native operation. The optional output "class label" for each sample:
\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N(indices of the most probable mixture component for each sample). It hasnsamples \times 1size and CV_32SC1 type.probsOutput destination, filled by the native operation. The optional output matrix that contains posterior probabilities of each Gaussian mixture component given the each sample. It has
nsamples \times nclusterssize and CV_64FC1 type.
The boolean result.
trainM
Estimate the Gaussian mixture parameters from a samples set.
This variation starts with Maximization step. You need to provide initial probabilities
`p_{i,k}` to use this option.
trainM(samples: Mat, probs0: Mat, logLikelihoods: Mat, labels: Mat, probs: Mat): boolean;4 available overloads
trainM(samples: Mat, probs0: Mat): boolean;trainM(samples: Mat, probs0: Mat, logLikelihoods: Mat): boolean;trainM(samples: Mat, probs0: Mat, logLikelihoods: Mat, labels: Mat): boolean;trainM(samples: Mat, probs0: Mat, logLikelihoods: Mat, labels: Mat, probs: Mat): boolean;samplesSamples from which the Gaussian mixture model will be estimated. It should be a one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.
probs0the probabilities
logLikelihoodsOutput destination, filled by the native operation. The optional output matrix that contains a likelihood logarithm value for each sample. It has
nsamples \times 1size and CV_64FC1 type.labelsOutput destination, filled by the native operation. The optional output "class label" for each sample:
\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N(indices of the most probable mixture component for each sample). It hasnsamples \times 1size and CV_32SC1 type.probsOutput destination, filled by the native operation. The optional output matrix that contains posterior probabilities of each Gaussian mixture component given the each sample. It has
nsamples \times nclusterssize and CV_64FC1 type.
The boolean 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.