KNearest

Objective-C

@interface KNearest : StatModel

Swift

class KNearest : StatModel

The class implements K-Nearest Neighbors model

See

REF: ml_intro_knn

Member of Ml

Methods

  • Creates the empty model

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

    Declaration

    Objective-C

    + (nonnull KNearest *)create;

    Swift

    class func create() -> KNearest
  • Loads and creates a serialized knearest from a file

    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.

    Declaration

    Objective-C

    + (nonnull KNearest *)load:(nonnull NSString *)filepath;

    Swift

    class func load(filepath: String) -> KNearest

    Parameters

    filepath

    path to serialized KNearest

  • Declaration

    Objective-C

    - (BOOL)getIsClassifier;

    Swift

    func getIsClassifier() -> Bool
  • Finds the neighbors and predicts responses for input vectors.

    Declaration

    Objective-C

    - (float)findNearest:(nonnull Mat *)samples
                        k:(int)k
                  results:(nonnull Mat *)results
        neighborResponses:(nonnull Mat *)neighborResponses
                     dist:(nonnull Mat *)dist;

    Swift

    func findNearest(samples: Mat, k: Int32, results: Mat, neighborResponses: Mat, dist: Mat) -> Float

    Parameters

    samples

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

    k

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

    results

    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

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

    dist

    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.

  • Finds the neighbors and predicts responses for input vectors.

    Declaration

    Objective-C

    - (float)findNearest:(nonnull Mat *)samples
                        k:(int)k
                  results:(nonnull Mat *)results
        neighborResponses:(nonnull Mat *)neighborResponses;

    Swift

    func findNearest(samples: Mat, k: Int32, results: Mat, neighborResponses: Mat) -> Float

    Parameters

    samples

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

    k

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

    results

    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

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

  • Finds the neighbors and predicts responses for input vectors.

    Declaration

    Objective-C

    - (float)findNearest:(nonnull Mat *)samples
                       k:(int)k
                 results:(nonnull Mat *)results;

    Swift

    func findNearest(samples: Mat, k: Int32, results: Mat) -> Float

    Parameters

    samples

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

    k

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

    results

    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. precision floating-point matrix of <number_of_samples> * k size. 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.

  • Declaration

    Objective-C

    - (int)getAlgorithmType;

    Swift

    func getAlgorithmType() -> Int32
  • Declaration

    Objective-C

    - (int)getDefaultK;

    Swift

    func getDefaultK() -> Int32
  • See

    -setEmax:

    Declaration

    Objective-C

    - (int)getEmax;

    Swift

    func getEmax() -> Int32
  • getAlgorithmType - see: -getAlgorithmType:

    Declaration

    Objective-C

    - (void)setAlgorithmType:(int)val;

    Swift

    func setAlgorithmType(val: Int32)
  • getDefaultK - see: -getDefaultK:

    Declaration

    Objective-C

    - (void)setDefaultK:(int)val;

    Swift

    func setDefaultK(val: Int32)
  • getEmax - see: -getEmax:

    Declaration

    Objective-C

    - (void)setEmax:(int)val;

    Swift

    func setEmax(val: Int32)
  • getIsClassifier - see: -getIsClassifier:

    Declaration

    Objective-C

    - (void)setIsClassifier:(BOOL)val;

    Swift

    func setIsClassifier(val: Bool)