OpenCV  5.0.0alpha
Open Source Computer Vision
Loading...
Searching...
No Matches
cv::ml::KNearest Class Referenceabstract

The class implements K-Nearest Neighbors model. More...

#include <opencv2/ml.hpp>

Collaboration diagram for cv::ml::KNearest:

Public Types

enum  Types {
  BRUTE_FORCE =1 ,
  KDTREE =2
}
 Implementations of KNearest algorithm. More...
 
- Public Types inherited from cv::ml::StatModel
enum  Flags {
  UPDATE_MODEL = 1 ,
  RAW_OUTPUT =1 ,
  COMPRESSED_INPUT =2 ,
  PREPROCESSED_INPUT =4
}
 

Public Member Functions

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.
 
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
 
- Public Member Functions inherited from cv::ml::StatModel
virtual float calcError (const Ptr< TrainData > &data, bool test, OutputArray resp) const
 Computes error on the training or test dataset.
 
virtual bool empty () const CV_OVERRIDE
 Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read.
 
virtual int getVarCount () const =0
 Returns the number of variables in training samples.
 
virtual bool isClassifier () const =0
 Returns true if the model is classifier.
 
virtual bool isTrained () const =0
 Returns true if the model is trained.
 
virtual float predict (InputArray samples, OutputArray results=noArray(), int flags=0) const =0
 Predicts response(s) for the provided sample(s)
 
virtual bool train (const Ptr< TrainData > &trainData, int flags=0)
 Trains the statistical model.
 
virtual bool train (InputArray samples, int layout, InputArray responses)
 Trains the statistical model.
 
- Public Member Functions inherited from cv::Algorithm
 Algorithm ()
 
virtual ~Algorithm ()
 
virtual void clear ()
 Clears the algorithm state.
 
virtual String getDefaultName () const
 
virtual void read (const FileNode &fn)
 Reads algorithm parameters from a file storage.
 
virtual void save (const String &filename) const
 
virtual void write (FileStorage &fs) const
 Stores algorithm parameters in a file storage.
 
void write (FileStorage &fs, const String &name) const
 

Static Public Member Functions

static Ptr< KNearestcreate ()
 Creates the empty model.
 
static Ptr< KNearestload (const String &filepath)
 Loads and creates a serialized knearest from a file.
 
- Static Public Member Functions inherited from cv::ml::StatModel
template<typename _Tp >
static Ptr< _Tptrain (const Ptr< TrainData > &data, int flags=0)
 Create and train model with default parameters.
 
- Static Public Member Functions inherited from cv::Algorithm
template<typename _Tp >
static Ptr< _Tpload (const String &filename, const String &objname=String())
 Loads algorithm from the file.
 
template<typename _Tp >
static Ptr< _TploadFromString (const String &strModel, const String &objname=String())
 Loads algorithm from a String.
 
template<typename _Tp >
static Ptr< _Tpread (const FileNode &fn)
 Reads algorithm from the file node.
 

Additional Inherited Members

- Protected Member Functions inherited from cv::Algorithm
void writeFormat (FileStorage &fs) const
 

Detailed Description

The class implements K-Nearest Neighbors model.

See also
K-Nearest Neighbors

Member Enumeration Documentation

◆ Types

Implementations of KNearest algorithm.

Enumerator
BRUTE_FORCE 
KDTREE 

Member Function Documentation

◆ create()

static Ptr< KNearest > cv::ml::KNearest::create ( )
static
Python:
cv.ml.KNearest.create() -> retval
cv.ml.KNearest_create() -> retval

Creates the empty model.

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

◆ findNearest()

virtual float cv::ml::KNearest::findNearest ( InputArray samples,
int k,
OutputArray results,
OutputArray neighborResponses = noArray(),
OutputArray dist = noArray() ) const
pure virtual
Python:
cv.ml.KNearest.findNearest(samples, k[, results[, neighborResponses[, dist]]]) -> retval, results, neighborResponses, dist

Finds the neighbors and predicts responses for input vectors.

Parameters
samplesInput samples stored by rows. It is a single-precision floating-point matrix of <number_of_samples> * k size.
kNumber of used nearest neighbors. Should be greater than 1.
resultsVector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with <number_of_samples> elements.
neighborResponsesOptional output values for corresponding neighbors. It is a single- precision floating-point matrix of <number_of_samples> * k size.
distOptional 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.

Here is the call graph for this function:

◆ getAlgorithmType()

virtual int cv::ml::KNearest::getAlgorithmType ( ) const
pure virtual
Python:
cv.ml.KNearest.getAlgorithmType() -> retval

Algorithm type, one of KNearest::Types.

See also
setAlgorithmType

◆ getDefaultK()

virtual int cv::ml::KNearest::getDefaultK ( ) const
pure virtual
Python:
cv.ml.KNearest.getDefaultK() -> retval

Default number of neighbors to use in predict method.

See also
setDefaultK

◆ getEmax()

virtual int cv::ml::KNearest::getEmax ( ) const
pure virtual
Python:
cv.ml.KNearest.getEmax() -> retval

Parameter for KDTree implementation.

See also
setEmax

◆ getIsClassifier()

virtual bool cv::ml::KNearest::getIsClassifier ( ) const
pure virtual
Python:
cv.ml.KNearest.getIsClassifier() -> retval

Whether classification or regression model should be trained.

See also
setIsClassifier

◆ load()

static Ptr< KNearest > cv::ml::KNearest::load ( const String & filepath)
static
Python:
cv.ml.KNearest.load(filepath) -> retval
cv.ml.KNearest_load(filepath) -> retval

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.

Parameters
filepathpath to serialized KNearest

◆ setAlgorithmType()

virtual void cv::ml::KNearest::setAlgorithmType ( int val)
pure virtual
Python:
cv.ml.KNearest.setAlgorithmType(val) -> None

◆ setDefaultK()

virtual void cv::ml::KNearest::setDefaultK ( int val)
pure virtual
Python:
cv.ml.KNearest.setDefaultK(val) -> None

See also
getDefaultK

◆ setEmax()

virtual void cv::ml::KNearest::setEmax ( int val)
pure virtual
Python:
cv.ml.KNearest.setEmax(val) -> None

See also
getEmax

◆ setIsClassifier()

virtual void cv::ml::KNearest::setIsClassifier ( bool val)
pure virtual
Python:
cv.ml.KNearest.setIsClassifier(val) -> None

See also
getIsClassifier

The documentation for this class was generated from the following file: