class cv::ml::KNearest

Overview

The class implements K-Nearest Neighbors model. More…

#include <ml.hpp>

class KNearest: public cv::ml::StatModel
{
public:
    // enums

    enum Types;

    // methods

    virtual
    float
    findNearest(
        InputArray samples,
        int k,
        OutputArray results,
        OutputArray neighborResponses = noArray(),
        OutputArray dist = noArray()
        ) const = 0;

    virtual
    int
    getAlgorithmType() const = 0;

    virtual
    int
    getDefaultK() const = 0;

    virtual
    int
    getEmax() const = 0;

    virtual
    bool
    getIsClassifier() const = 0;

    virtual
    void
    setAlgorithmType(int val) = 0;

    virtual
    void
    setDefaultK(int val) = 0;

    virtual
    void
    setEmax(int val) = 0;

    virtual
    void
    setIsClassifier(bool val) = 0;

    static
    Ptr<KNearest>
    create();
};

Inherited Members

public:
    // enums

    enum Flags;

    // methods

    virtual
    void
    clear();

    virtual
    bool
    empty() const;

    virtual
    String
    getDefaultName() const;

    virtual
    void
    read(const FileNode& fn);

    virtual
    void
    save(const String& filename) const;

    virtual
    void
    write(FileStorage& fs) const;

    template <typename _Tp>
    static
    Ptr<_Tp>
    load(
        const String& filename,
        const String& objname = String()
        );

    template <typename _Tp>
    static
    Ptr<_Tp>
    loadFromString(
        const String& strModel,
        const String& objname = String()
        );

    template <typename _Tp>
    static
    Ptr<_Tp>
    read(const FileNode& fn);

    virtual
    float
    calcError(
        const Ptr<TrainData>& data,
        bool test,
        OutputArray resp
        ) const;

    virtual
    bool
    empty() const;

    virtual
    int
    getVarCount() const = 0;

    virtual
    bool
    isClassifier() const = 0;

    virtual
    bool
    isTrained() const = 0;

    virtual
    float
    predict(
        InputArray samples,
        OutputArray results = noArray(),
        int flags = 0
        ) const = 0;

    virtual
    bool
    train(
        const Ptr<TrainData>& trainData,
        int flags = 0
        );

    virtual
    bool
    train(
        InputArray samples,
        int layout,
        InputArray responses
        );

    template <typename _Tp>
    static
    Ptr<_Tp>
    train(
        const Ptr<TrainData>& data,
        int flags = 0
        );

protected:
    // methods

    void
    writeFormat(FileStorage& fs) const;

Detailed Documentation

The class implements K-Nearest Neighbors model.

See also:

K-Nearest Neighbors

Methods

virtual
float
findNearest(
    InputArray samples,
    int k,
    OutputArray results,
    OutputArray neighborResponses = noArray(),
    OutputArray dist = noArray()
    ) const = 0

Finds the neighbors and predicts responses for input vectors.

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.

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.
virtual
int
getAlgorithmType() const = 0

Algorithm type, one of KNearest::Types.

See also:

setAlgorithmType

virtual
int
getDefaultK() const = 0

Default number of neighbors to use in predict method.

See also:

setDefaultK

virtual
int
getEmax() const = 0

Parameter for KDTree implementation.

See also:

setEmax

virtual
bool
getIsClassifier() const = 0

Whether classification or regression model should be trained.

See also:

setIsClassifier

virtual
void
setAlgorithmType(int val) = 0

See also:

getAlgorithmType

virtual
void
setDefaultK(int val) = 0

See also:

getDefaultK

virtual
void
setEmax(int val) = 0

See also:

getEmax

virtual
void
setIsClassifier(bool val) = 0

See also:

getIsClassifier

static
Ptr<KNearest>
create()

Creates the empty model.

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