View Source Evision.ML.KNearest (Evision v0.1.21)

Link to this section Summary

Types

t()

Type that represents an Evision.ML.KNearest struct.

Functions

Computes error on the training or test dataset

Computes error on the training or test dataset

Clears the algorithm state

Creates the empty model

empty

Finds the neighbors and predicts responses for input vectors.

Finds the neighbors and predicts responses for input vectors.

getAlgorithmType

getDefaultK

getDefaultName

getEmax

getIsClassifier

Returns the number of variables in training samples

Returns true if the model is classifier

Returns true if the model is trained

Loads and creates a serialized knearest from a file

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

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

Reads algorithm parameters from a file storage

setAlgorithmType

setIsClassifier

Trains the statistical model

Trains the statistical model

Trains the statistical model

simplified API for language bindings

simplified API for language bindings

Link to this section Types

@type t() :: %Evision.ML.KNearest{ref: reference()}

Type that represents an Evision.ML.KNearest struct.

  • ref. reference()

    The underlying erlang resource variable.

Link to this section Functions

Link to this function

calcError(self, data, test)

View Source
@spec calcError(t(), Evision.ML.TrainData.t(), boolean()) ::
  {number(), Evision.Mat.t()} | {:error, String.t()}

Computes error on the training or test dataset

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • data: Evision.ML.TrainData.

    the training data

  • test: bool.

    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.

Return
  • retval: float

  • resp: Evision.Mat.

    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%).

Python prototype (for reference only):

calcError(data, test[, resp]) -> retval, resp
Link to this function

calcError(self, data, test, opts)

View Source
@spec calcError(
  t(),
  Evision.ML.TrainData.t(),
  boolean(),
  [{atom(), term()}, ...] | nil
) ::
  {number(), Evision.Mat.t()} | {:error, String.t()}

Computes error on the training or test dataset

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • data: Evision.ML.TrainData.

    the training data

  • test: bool.

    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.

Return
  • retval: float

  • resp: Evision.Mat.

    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%).

Python prototype (for reference only):

calcError(data, test[, resp]) -> retval, resp
@spec clear(t()) :: :ok | {:error, String.t()}

Clears the algorithm state

Positional Arguments
  • self: Evision.ML.KNearest.t()

Python prototype (for reference only):

clear() -> None
@spec create() :: t() | {:error, String.t()}

Creates the empty model

Return

The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.

Python prototype (for reference only):

create() -> retval
@spec empty(t()) :: boolean() | {:error, String.t()}

empty

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: bool

Python prototype (for reference only):

empty() -> retval
Link to this function

findNearest(self, samples, k)

View Source
@spec findNearest(t(), Evision.Mat.maybe_mat_in(), integer()) ::
  {number(), Evision.Mat.t(), Evision.Mat.t(), Evision.Mat.t()}
  | {:error, String.t()}

Finds the neighbors and predicts responses for input vectors.

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • samples: Evision.Mat.

    Input samples stored by rows. It is a single-precision floating-point matrix of <number_of_samples> * k size.

  • k: int.

    Number of used nearest neighbors. Should be greater than 1.

Return
  • retval: float

  • results: Evision.Mat.

    Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with <number_of_samples> elements.

  • neighborResponses: Evision.Mat.

    Optional output values for corresponding neighbors. It is a single- precision floating-point matrix of <number_of_samples> * k size.

  • dist: Evision.Mat.

    Optional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of <number_of_samples> * k size.

For each input vector (a row of the matrix samples), the method finds the k nearest neighbors. In case of regression, the predicted result is a mean value of the particular vector's neighbor responses. In case of classification, the class is determined by voting. For each input vector, the neighbors are sorted by their distances to the vector. In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself. If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method. The function is parallelized with the TBB library.

Python prototype (for reference only):

findNearest(samples, k[, results[, neighborResponses[, dist]]]) -> retval, results, neighborResponses, dist
Link to this function

findNearest(self, samples, k, opts)

View Source
@spec findNearest(
  t(),
  Evision.Mat.maybe_mat_in(),
  integer(),
  [{atom(), term()}, ...] | nil
) ::
  {number(), Evision.Mat.t(), Evision.Mat.t(), Evision.Mat.t()}
  | {:error, String.t()}

Finds the neighbors and predicts responses for input vectors.

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • samples: Evision.Mat.

    Input samples stored by rows. It is a single-precision floating-point matrix of <number_of_samples> * k size.

  • k: int.

    Number of used nearest neighbors. Should be greater than 1.

Return
  • retval: float

  • results: Evision.Mat.

    Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with <number_of_samples> elements.

  • neighborResponses: Evision.Mat.

    Optional output values for corresponding neighbors. It is a single- precision floating-point matrix of <number_of_samples> * k size.

  • dist: Evision.Mat.

    Optional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of <number_of_samples> * k size.

For each input vector (a row of the matrix samples), the method finds the k nearest neighbors. In case of regression, the predicted result is a mean value of the particular vector's neighbor responses. In case of classification, the class is determined by voting. For each input vector, the neighbors are sorted by their distances to the vector. In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself. If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method. The function is parallelized with the TBB library.

Python prototype (for reference only):

findNearest(samples, k[, results[, neighborResponses[, dist]]]) -> retval, results, neighborResponses, dist
@spec getAlgorithmType(t()) :: integer() | {:error, String.t()}

getAlgorithmType

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: int

@see setAlgorithmType/2

Python prototype (for reference only):

getAlgorithmType() -> retval
@spec getDefaultK(t()) :: integer() | {:error, String.t()}

getDefaultK

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: int

@see setDefaultK/2

Python prototype (for reference only):

getDefaultK() -> retval
@spec getDefaultName(t()) :: binary() | {:error, String.t()}

getDefaultName

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return

Returns the algorithm string identifier. This string is used as top level xml/yml node tag when the object is saved to a file or string.

Python prototype (for reference only):

getDefaultName() -> retval
@spec getEmax(t()) :: integer() | {:error, String.t()}

getEmax

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: int

@see setEmax/2

Python prototype (for reference only):

getEmax() -> retval
@spec getIsClassifier(t()) :: boolean() | {:error, String.t()}

getIsClassifier

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: bool

@see setIsClassifier/2

Python prototype (for reference only):

getIsClassifier() -> retval
@spec getVarCount(t()) :: integer() | {:error, String.t()}

Returns the number of variables in training samples

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: int

Python prototype (for reference only):

getVarCount() -> retval
@spec isClassifier(t()) :: boolean() | {:error, String.t()}

Returns true if the model is classifier

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: bool

Python prototype (for reference only):

isClassifier() -> retval
@spec isTrained(t()) :: boolean() | {:error, String.t()}

Returns true if the model is trained

Positional Arguments
  • self: Evision.ML.KNearest.t()
Return
  • retval: bool

Python prototype (for reference only):

isTrained() -> retval
@spec load(binary()) :: t() | {:error, String.t()}

Loads and creates a serialized knearest from a file

Positional Arguments
  • filepath: String.

    path to serialized KNearest

Return

Use KNearest::save to serialize and store an KNearest to disk. Load the KNearest from this file again, by calling this function with the path to the file.

Python prototype (for reference only):

load(filepath) -> retval
@spec predict(t(), Evision.Mat.maybe_mat_in()) ::
  {number(), Evision.Mat.t()} | {:error, String.t()}

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

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • samples: Evision.Mat.

    The input samples, floating-point matrix

Keyword Arguments
  • flags: int.

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

Return
  • retval: float

  • results: Evision.Mat.

    The optional output matrix of results.

Python prototype (for reference only):

predict(samples[, results[, flags]]) -> retval, results
Link to this function

predict(self, samples, opts)

View Source
@spec predict(t(), Evision.Mat.maybe_mat_in(), [{atom(), term()}, ...] | nil) ::
  {number(), Evision.Mat.t()} | {:error, String.t()}

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

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • samples: Evision.Mat.

    The input samples, floating-point matrix

Keyword Arguments
  • flags: int.

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

Return
  • retval: float

  • results: Evision.Mat.

    The optional output matrix of results.

Python prototype (for reference only):

predict(samples[, results[, flags]]) -> retval, results
@spec read(t(), Evision.FileNode.t()) :: :ok | {:error, String.t()}

Reads algorithm parameters from a file storage

Positional Arguments

Python prototype (for reference only):

read(fn_) -> None
@spec save(t(), binary()) :: :ok | {:error, String.t()}

save

Positional Arguments
  • self: Evision.ML.KNearest.t()
  • filename: String

Saves the algorithm to a file. In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).

Python prototype (for reference only):

save(filename) -> None
Link to this function

setAlgorithmType(self, val)

View Source
@spec setAlgorithmType(t(), integer()) :: :ok | {:error, String.t()}

setAlgorithmType

Positional Arguments
  • self: Evision.ML.KNearest.t()
  • val: int

@see getAlgorithmType/1

Python prototype (for reference only):

setAlgorithmType(val) -> None
@spec setDefaultK(t(), integer()) :: :ok | {:error, String.t()}

setDefaultK

Positional Arguments
  • self: Evision.ML.KNearest.t()
  • val: int

@see getDefaultK/1

Python prototype (for reference only):

setDefaultK(val) -> None
@spec setEmax(t(), integer()) :: :ok | {:error, String.t()}

setEmax

Positional Arguments
  • self: Evision.ML.KNearest.t()
  • val: int

@see getEmax/1

Python prototype (for reference only):

setEmax(val) -> None
Link to this function

setIsClassifier(self, val)

View Source
@spec setIsClassifier(t(), boolean()) :: :ok | {:error, String.t()}

setIsClassifier

Positional Arguments
  • self: Evision.ML.KNearest.t()
  • val: bool

@see getIsClassifier/1

Python prototype (for reference only):

setIsClassifier(val) -> None
@spec train(t(), Evision.ML.TrainData.t()) :: boolean() | {:error, String.t()}

Trains the statistical model

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • trainData: Evision.ML.TrainData.

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

Keyword Arguments
  • flags: int.

    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).

Return
  • retval: bool

Python prototype (for reference only):

train(trainData[, flags]) -> retval
Link to this function

train(self, trainData, opts)

View Source
@spec train(t(), Evision.ML.TrainData.t(), [{atom(), term()}, ...] | nil) ::
  boolean() | {:error, String.t()}

Trains the statistical model

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • trainData: Evision.ML.TrainData.

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

Keyword Arguments
  • flags: int.

    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).

Return
  • retval: bool

Python prototype (for reference only):

train(trainData[, flags]) -> retval
Link to this function

train(self, samples, layout, responses)

View Source
@spec train(t(), Evision.Mat.maybe_mat_in(), integer(), Evision.Mat.maybe_mat_in()) ::
  boolean() | {:error, String.t()}

Trains the statistical model

Positional Arguments
  • self: Evision.ML.KNearest.t()

  • samples: Evision.Mat.

    training samples

  • layout: int.

    See ml::SampleTypes.

  • responses: Evision.Mat.

    vector of responses associated with the training samples.

Return
  • retval: bool

Python prototype (for reference only):

train(samples, layout, responses) -> retval
@spec write(t(), Evision.FileStorage.t()) :: :ok | {:error, String.t()}

simplified API for language bindings

Positional Arguments
Keyword Arguments

Has overloading in C++

Python prototype (for reference only):

write(fs[, name]) -> None
@spec write(t(), Evision.FileStorage.t(), [{atom(), term()}, ...] | nil) ::
  :ok | {:error, String.t()}

simplified API for language bindings

Positional Arguments
Keyword Arguments

Has overloading in C++

Python prototype (for reference only):

write(fs[, name]) -> None